Skip to content

Instantly share code, notes, and snippets.

@vovs03
Created June 7, 2018 20:40
Show Gist options
  • Save vovs03/4b01b7e2b3d8048f0ac4dc9232cd24df to your computer and use it in GitHub Desktop.
Save vovs03/4b01b7e2b3d8048f0ac4dc9232cd24df to your computer and use it in GitHub Desktop.
Bubble sorting in Ruby
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