Created
June 11, 2020 19:01
-
-
Save violetguos/c9747ec19f8b4e34fff1c46177e26aa8 to your computer and use it in GitHub Desktop.
TOP bubble sort assignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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