19/19 How do you do this exercise using "if/elsif/else" statements?


#1

I can do the combined comparison operator one without any trouble


#2

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"]

Sorting


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.