Skip to content

Instantly share code, notes, and snippets.

@MagnusRufum
Created June 18, 2018 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MagnusRufum/e3f584444e698785fc65085309bdd5a2 to your computer and use it in GitHub Desktop.
Save MagnusRufum/e3f584444e698785fc65085309bdd5a2 to your computer and use it in GitHub Desktop.
Bubble Sort Ruby solution for The Odin Project
def bubble_sort(array)
loop do
swapped = false
(array.length-1).times do |i|
if array[i] > array[i+1]
array[i], array[i+1] = array[i+1], array[i]
swapped = true
end
end
break if not swapped
end
array
end
def bubble_sort_by(array)
loop do
swapped = false
(array.length-1).times do |i|
if yield(array[i], array[i+1]) > 0
array[i], array[i+1] = array[i+1], array[i]
swapped = true
end
end
break if not swapped
end
array
end
puts (bubble_sort_by(["hello", "hey", "hi"]) do |left, right|
left.length - right.length
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment