Created
November 2, 2022 02:27
-
-
Save hepplerj/79bb21d9645de7fda4409e5e2b11ce87 to your computer and use it in GitHub Desktop.
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(arr) | |
for i in 0...arr.length | |
sorted = true | |
for k in 0...(arr.length - i - 1) | |
if arr[k] > arr[k + 1] | |
arr[k], arr[k + 1] = arr[k + 1], arr[k] | |
sorted = false | |
end | |
end | |
break if sorted | |
end | |
arr | |
end | |
data = [10, 2, 6, 2, 1, 5, 8, 6, 0, 2, 8, 9] | |
p bubble_sort(data) | |
# => [0, 1, 2, 2, 2, 5, 6, 6, 8, 8, 9, 10] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment