Skip to content

Instantly share code, notes, and snippets.

@everylittlefox
Created August 7, 2022 13:33
Show Gist options
  • Select an option

  • Save everylittlefox/26a49c4c7e62051339c839a377aede35 to your computer and use it in GitHub Desktop.

Select an option

Save everylittlefox/26a49c4c7e62051339c839a377aede35 to your computer and use it in GitHub Desktop.
def bubble_sort(array)
loop do
swapped = false
for i in (1...array.length)
if array[i-1] > array[i]
array[i-1], array[i] = array[i], array[i-1]
swapped = true
end
end
break unless swapped
end
array
end
p bubble_sort([4,3,78,2,0,2])
#=> [0,2,2,3,4,78]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment