class ModifiedBubbleSort

Public Class Methods

run(data) click to toggle source
# File lib/compare-sort.rb, line 146
def self.run(data)
        sorted = false 

        while !sorted 
                sorted = true
                # iterate through the whole array
                (data.length - 1).times  do |i|
                        # if the element ahead of the one we are on is smaller
                        # then switch them
                        if (data[i] > data[i+1])
                                data[i+1], data[i] = data[i], data[i+1]
                                sorted = false 
                        end
                end 
        end
        return data
end