Question on sorting (.19 sorting)


#1



exercise 19 sorting


my code seems to work. my question is why would i do if,elsif,else when putting .sort!reverse! does it just as well. is there an advantage to using if,elsif,else and if so please help me understand how to use it when sorting. below is the code i used and it did perform the task.


fruits = ["orange", "apple", "banana", "pear", "grapes"]
fruits.sort!.reverse! do |ff, sf|
    ff <=> sf
end


#2

It's either one way or the other, but not both. Your code above is sorting in ascending order in the do..end loop, and then reverse! reverses the order, which can be done in the do..end loop.

fruits.sort! do |ff, sf|
    sf <=> ff
end
# ["pear", "orange", "grapes", "banana", "apple"]

#3

gotcha. makes sense now. They also say you can use if, elsif, else to accomplish the task. Is one better than the other or do they do the same thing just a do..end rather than an if, elsif, else?


#4

The built-in methods are concise and fast, and easy to read. .sort! and .reverse! are great off the shelf solutions. But, it doesn't help if we do not understand how they work. That's why the topic of sorting is so interesting and deserves a good deal of study and practice so we teach our minds how to visualize the process and create our own solutions.

Sorting manually with if/elsif.else is a fairly complicated process. It still requires a loop to cycle through the iterable.

Consider,

fruits = ["orange", "apple", "banana", "pear", "grapes"]

for x in 0...fruits.length - 1
    if fruits[x] > fruits[x + 1]
        temp = fruits[x]
        fruits[x] = fruits[x + 1]
        fruits[x + 1] = temp
    end
end

It will take more than one pass so we need some way to loop back to the beginning and run the loop again. For this, we need to introduce a control. More later. Here is the result of the first pass.

["apple", "banana", "orange", "grapes', "pear"]

So let's look at how we can control the outer loop that is needed so the for loop can be run over and over, as needed.

fruits = ["orange", "apple", "banana", "pear", "grapes"]
begin
    fruits.each {|x| print "#{x}, "}
    puts
    noswap = true
    for x in 0...fruits.length - 1
        if fruits[x] > fruits[x + 1]
            temp = fruits[x]
            fruits[x] = fruits[x + 1]
            fruits[x + 1] = temp
            noswap = false
        end
    end
end until noswap == true

Output

orange, apple, banana, pear, grapes, 
apple, banana, orange, grapes, pear, 
apple, banana, grapes, orange, pear,

This is known as a bubble sort and probably the least used sort method except for short lists. The inefficiency begins to show when the list is longer. We still haven't looked at if-elsif-else which is more or less an underlying principle of some sorting methods.

def before_after_same(a, b)
    if a < b
        return -1
    elsif a > b
        return 1
    else
        return 0
end

I'll leave you to study up some more on this, before I end up out of my own depth.


#5

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