Python Program to Implement Insertion Sort



In this program, we will implement Insertion Sort



  1. #Python program to implement Insertion sort
  2. def insertion_sort(a):
  3. for i in range(1, len(a)):
  4. temp = a[i]
  5. j = i - 1
  6. while (j >= 0 and temp < a[j]):
  7. a[j + 1] = a[j]
  8. j = j - 1
  9. a[j + 1] = temp
  10. a = input('Enter list elements: ').split()
  11. a = [int(x) for x in a]
  12. insertion_sort(a)
  13. print('Sorted list: ', end='')
  14. print(a)

Output


Output!