Skip to content

Instantly share code, notes, and snippets.

@Bablzz
Created December 13, 2019 13:30
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 Bablzz/98875444548100fde2a299662ca185db to your computer and use it in GitHub Desktop.
Save Bablzz/98875444548100fde2a299662ca185db to your computer and use it in GitHub Desktop.
Insertion sort
# O(n^2)
def insertion_sort arr
i = 0
j = 0
for i in 1...(arr.length) do
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j] do
arr[j + 1] = arr[j]
j -= 1
end
arr[j + 1] = key
end
arr
end
arra = [1,6,0,5,12]
puts insertion_sort(arra)
# => 0 1 5 6 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment