Skip to content

Instantly share code, notes, and snippets.

@steelThread
Created November 5, 2012 16:19
Show Gist options
  • Save steelThread/4018073 to your computer and use it in GitHub Desktop.
Save steelThread/4018073 to your computer and use it in GitHub Desktop.
bored...
class Array
def swap!(a, b)
self[a], self[b] = self[b], self[a]
end
def bubble_sort
sorted = self.dup
return sorted unless sorted.count > 1
count = sorted.count-1
(0...count).each do |i|
(0...count-i).each do |j|
sorted.swap!(j, j+1) if sorted[j] > sorted[j+1]
end
end
sorted
end
end
puts 10.times.map { 20+Random.rand(11) }.bubble_sort
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment