Created
March 13, 2017 14:46
-
-
Save aspencer8111/415ce314b83c0f20345259aa6c375457 to your computer and use it in GitHub Desktop.
A simple bubble sort algorithm in ruby
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(array) | |
loop do | |
swapped = false | |
(array.length-1).times do |i| | |
if array[i] > array[i + 1] | |
array[i], array[i + 1] = array[i + 1], array[i] | |
swapped = true | |
end | |
end | |
break if not swapped | |
end | |
array | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment