Class: ModifiedBubbleSort

Inherits:
Object
  • Object
show all
Defined in:
lib/compare-sort.rb

Class Method Summary collapse

Class Method Details

.run(data) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# 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