arrays - Ruby, Working on a recursive bubble sort method and totally stuck -


so i'm doing chris pine's "learn program", , i've been having no end of trouble writing own recursive sorting method.

i have been making slow (very slow) progress trial , error...but i'm stuck , have no clue. here's code: (and know got lots of other problems, work out in time, want answer question asked below, thanks)

array = ['v', 't','k','l','w','o','a','y','p','f','x','g','h','j','z'] sorted_array = []  def mysort(array, sorted_array)     if array.length <= 0         return     end      x = 0     y = 0      while y < array.length          if array[x] < array[y]             y += 1         elsif array[x] > array[y]             x += y             y += 1         else             y += 1         end     end      sorted_array.push(array[x])     array.delete(array[x])     puts "test complete" end  mysort(array, sorted_array) 

if array given 'b' first element instead of 'v' works fine, otherwise while loop skipped , error "undefined method '<' nil:nilclass" suspect problem has using #delete method on array @ end, really dont understand why while loop skipped on depending on elements in array?!? doesnt make sense me, since while loop's condition not based on string being tested.

like said earlier, i'm aware there lots of other problems fix before working sort method (the recursion one), i'm going try work rest out on own, 1 thing totally eluding me.

thanks!

edit

so here post fix code in working order might find later , benefit it....

array = ['v', 't', 'k', 'l', 'w', 'o', 'a', 'y', 'p', 'f', 'x', 'g', 'h', 'j', 'z'] sorted_array = []  def mysort(array, sorted_array)     if array.length <= 0         return     end     x = 0     y = 0     while y < array.length          if array[x] < array[y]             y += 1         elsif array[x] > array[y]             x = y             y += 1         else             y += 1         end     end     sorted_array.push(array[x])      array.delete(array[x])     mysort(array, sorted_array) end mysort(array, sorted_array)  puts sorted_array puts puts array 

i assume forgot put call mysort @ end of function definition, since without it, comparisons go through array once. , without call inside, there's no nil:nilclass error. me, appearance of error doesn't depend on weather first element 'b' or 'v', although changes iteration program fails on.

this isn't bubble sort @ all, it's selection sort.

and here's the culprit:

x += y 

should be:

x = y 

if have first version, x can get bigger array , therefore out of bound. trying element out of bounds of array results in nil. code tried execute (for example):

'a' < nil 

and that's error hinted at: objects of class nilclass (the class of nil, surprisingly) don't have '<' method. nil doesn't have comparison implemented, because there's no result can returned. if code structured differently, interpreter tell "comparison of string nil failed", tells same thing in other words.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -