I was doing the recursive sorting program in Chapter 10 of Chris
Pine's learning to program and i wrote this:
def sort some_array
recursive_sort some_array, []
end
def recursive_sort unsorted_array, sorted_array
if unsorted_array.length != 0
0.upto(unsorted_array.length-2) do |i|
if (unsorted_array[i+1] != nil) and (unsorted_array[i] <
unsorted_array[i+1])
unsorted_array[i], unsorted_array[i+1] = unsorted_array[i+1],
unsorted_array[i]
end
end
sorted_array.push(unsorted_array.last)
unsorted_array.pop
sort(unsorted_array)
end
if unsorted_array.length==0
puts sorted_array
end
end
word=[]
puts 'Input Words'
while word.last!=''
word.push gets.chomp.downcase
end
sort(word)
···
---------------------------------------------------------
what i don't get is that this program sorts the words into reverse
alphabetical order.
but if i switch the less than sign in "if (unsorted_array[i+1] != nil)
and (unsorted_array[i] < unsorted_array[i+1])" to a greater than sign
the program works correctly.
what i am trying to is move the smallest word into the sorted_array
but if i use the greater than sign wouldn't the move the biggest word
into the sorted_array?
i'm really new to this, so please pardon my misunderstanding.