Skip to content

Instantly share code, notes, and snippets.

@violetguos
Created June 11, 2020 19:01
Show Gist options
  • Save violetguos/c9747ec19f8b4e34fff1c46177e26aa8 to your computer and use it in GitHub Desktop.
Save violetguos/c9747ec19f8b4e34fff1c46177e26aa8 to your computer and use it in GitHub Desktop.
TOP bubble sort assignment
def bubble_sort(arr)
# drop ignores previous sorted elems
arr.each_with_index do |dum, drop_i|
arr.drop(drop_i).each_with_index do |elem, i|
if arr[i+1] != nil && arr[i] > arr[i + 1]
temp = arr[i]
arr[i] = arr[i+1]
arr[i+1] = temp
end
end
end
return arr
end
puts bubble_sort([4,3,78,2,0,2]).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment