Skip to content

Instantly share code, notes, and snippets.

@bitops
Created July 27, 2016 05:29
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 bitops/6b52a0c323c801c744fdb9f042f2609f to your computer and use it in GitHub Desktop.
Save bitops/6b52a0c323c801c744fdb9f042f2609f to your computer and use it in GitHub Desktop.
bubble sort :)
def bubble_sort(array)
@swap_needed = false
array.each_with_index do |val, idx|
a = val
b = array[idx + 1]
if !b.nil? && a > b
@swap_needed = true
array[idx] = b
array[idx + 1] = a
end
end
@swap_needed ? bubble_sort(array) : array
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment