Skip to content

Instantly share code, notes, and snippets.

@kirqe
Last active August 29, 2015 14:12
Show Gist options
  • Save kirqe/6ccebdb4a143ddba6bec to your computer and use it in GitHub Desktop.
Save kirqe/6ccebdb4a143ddba6bec to your computer and use it in GitHub Desktop.
bubble sort
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