I can do the combined comparison operator one without any trouble
Sorting with an if/elsif/else
statement is a rather drawn out process with several approaches at our disposal. The simplest is the bubble sort.
def bubble_sort(sample)
for i in (0..sample.length)
for j in (i..sample.length - 1).reverse_each
if sample[i] > sample[j]
sample[i], sample[j] = sample[j], sample[i]
end
end
end
return sample
end
puts bubble_sort(fruits)
["apple", "banana", "grapes", "orange", "pear"]
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.