Class: Algorithmable::Sort::Bubble

Inherits:
Object
  • Object
show all
Defined in:
lib/algorithmable/sort/bubble.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sort(collection) ⇒ Object



4
5
6
# File 'lib/algorithmable/sort/bubble.rb', line 4

def self.sort(collection)
  new.sort(collection)
end

Instance Method Details

#sort(collection) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/algorithmable/sort/bubble.rb', line 8

def sort(collection)
  loop do
    swapped = false
    (collection.size.pred).times do |index|
      if (collection[index] <=> collection[index.next]) == 1
        collection = swap(index, collection)
        swapped = true
      end
    end
    break unless swapped
  end
  collection
end