Skip to content

Instantly share code, notes, and snippets.

@hepplerj
Created November 2, 2022 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hepplerj/79bb21d9645de7fda4409e5e2b11ce87 to your computer and use it in GitHub Desktop.
Save hepplerj/79bb21d9645de7fda4409e5e2b11ce87 to your computer and use it in GitHub Desktop.
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