Created
June 7, 2018 20:40
-
-
Save vovs03/4b01b7e2b3d8048f0ac4dc9232cd24df to your computer and use it in GitHub Desktop.
Bubble sorting 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 bubbleSort(arr) | |
swapped = true | |
for i in 0 ... (arr.length - 1) | |
swapped = false | |
for j in 0 ... (arr.length - i - 1) | |
if arr[j] > arr[j + 1] | |
arr[j], arr[j + 1] = arr[j + 1], arr[j] | |
swapped = true | |
end | |
end | |
break if swapped == false | |
end | |
end | |
nums = [3, 2, 1, 5, 21, 8, 6, 9, 4, 7] | |
bubbleSort(nums) | |
p nums |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment