Created
February 25, 2021 08:20
-
-
Save MBM1607/0ff37d842dd8013a49e74a7ed369c9f5 to your computer and use it in GitHub Desktop.
bubble sort with ruby
This file contains 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(array) | |
sorted = false | |
until sorted do | |
sorted = true | |
array.each_with_index do |_, i| | |
if i + 1 != array.length && array[i] > array[i + 1] | |
array[i], array[i + 1] = array[i + 1], array[i] | |
sorted = false | |
end | |
end | |
end | |
return array | |
end | |
p bubble_sort([4,3,78,2,0,2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment