Вопрос

So I'm doing Chris Pine's "Learn to Program", and I've been having no end of trouble writing my own recursive sorting method.

I have been making slow (very slow) progress by trial and error...but now I'm stuck and have no clue. Here's the code: (and I know its got lots of other problems, but I will work those out in time, just want the answer the 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 the array is given 'b' for the first element instead of 'v' it works fine, Otherwise my while loop is skipped and I get the error "undefined method '<' for nil:NilClass" I strongly suspect the problem has something to do with my using the #delete method on array at the end, but what I really really dont understand is why my while loop is skipped over depending on the elements in my array?!? It doesnt make sense to me, since the while loop's condition is not based on what string is being tested.

Like I said earlier, I'm aware there are lots of other problems to fix before this is a working sort method (the recursion for one), but I'm going to try to work the rest out on my own, its just this one thing that is totally eluding me.

Thanks!

EDIT

So here is the post fix code in working order for anyone who might find this later and benefit from 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 you forgot to put the call to mySort at the end of function definition, since without it, the comparisons only go through the array once. And without that call inside, there's no nil:NilClass error. For me, the appearance of the error doesn't depend on weather the first element is 'b' or 'v', although that changes which iteration the program fails on.

This isn't a bubble sort at all, it's a selection sort.

And here's the culprit:

x += y

should be:

x = y

If you have the first version, x can get get bigger than the array and therefore out of bound. Trying to get an element out of bounds of an array results in a nil. So your code tried to execute (for example):

'a' < nil

And that's exactly what the error hinted at: objects of class NilClass (the class of nil, surprisingly) don't have the '<' method. nil doesn't have a comparison implemented, because there's no good result that can be returned. If your code was structured differently, the interpreter would tell you that the "comparison of String with nil failed", which tells us the same thing in other words.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top