Skip to content

Instantly share code, notes, and snippets.

@Neptune998
Last active August 13, 2020 18:18
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 Neptune998/25a0f16a11ae8ce8de17d089525971e7 to your computer and use it in GitHub Desktop.
Save Neptune998/25a0f16a11ae8ce8de17d089525971e7 to your computer and use it in GitHub Desktop.
Insertion Sort
# Insertion sort
def Insertion_sort(arr, itr):
lgth = len(arr)
for i in range(1,lgth):
x,j = arr[i],i-1
while j>=0 and arr[j]>x:
arr[j+1] = arr[j]
j-=1
arr[j+1] = x
print("Iteration:",i,arr)
itr+=1
return arr, itr
if __name__ == '__main__':
# arr = list(map(int, input("Enter array:").split()))
arr = [5,4,3,2,1]
sorted_arr, itr = Insertion_sort(arr, itr=0)
print("Total Iterations/Swaps:",itr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment