Last active
August 29, 2015 14:12
-
-
Save kirqe/6ccebdb4a143ddba6bec to your computer and use it in GitHub Desktop.
bubble sort
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
nums = [3,4,2,6,1] | |
def bubble_sort(arr) | |
sorted = false | |
until sorted | |
sorted = true | |
(arr.count - 1).times do |i| | |
if arr[i] > arr[i + 1] | |
arr[i], arr[i + 1] = arr[i + 1], arr[i] | |
sorted = false | |
end | |
end | |
end | |
arr | |
end | |
p bubble_sort(nums) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment