Skip to content

Instantly share code, notes, and snippets.

@san9dev
Created November 6, 2019 01:57
Show Gist options
  • Save san9dev/dfaa4ce579d60470a7fbfc65f7069d98 to your computer and use it in GitHub Desktop.
Save san9dev/dfaa4ce579d60470a7fbfc65f7069d98 to your computer and use it in GitHub Desktop.
bubule sort with ruby
# buble sort with ruby
def bublesort(arr)
n = arr.length - 1
sorted = true
while sorted do
sorted = false
n.times do |i|
if arr[i] > arr[i + 1]
arr[i], arr[i + 1] = arr[i + 1], arr[i] # exchange array elements
sorted = true
end
end
end
arr
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment