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(&#x27;Enter list elements: &#x27;).split()
  11. a = [int(x) for x in a]
  12. insertion_sort(a)
  13. print(&#x27;Sorted list: &#x27;, end=&#x27;&#x27;)
  14. print(a)

Output
Output!




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube