Skip to content

Instantly share code, notes, and snippets.

@DanielAmah
Last active July 11, 2019 06:02
Show Gist options
  • Select an option

  • Save DanielAmah/5ce961b4c1fbaadd5511c641f565e065 to your computer and use it in GitHub Desktop.

Select an option

Save DanielAmah/5ce961b4c1fbaadd5511c641f565e065 to your computer and use it in GitHub Desktop.
# implementation of insertion sort
# Pick an item from an array
# compare all items in the sorted sub-list
# shift all items in the sorted sub-list greater than the item to be sorted
# insert the item
# repeat until the list is sorted.
def insertion_sort(arr)
a_length = arr.length
a_length.times do |i|
while i > 0
if arr[i - 1] > arr[i]
arr[i], arr[i - 1] = arr[i - 1], arr[i]
else
break
end
i -= 1
end
end
arr
end
arr = [2, 5, 6, 2, 5425, 54, 5, 4, 12, 7, 3, 5, 5]
print insertion_sort(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment