Python program for Insertion sort

Table of Contents

Insertion sort

You can also check following sort

Author : ITVoyagers (

Date :26th October 2019

Description : Program to show implementation of insertion sort in python
def insertionsort(collection):

   for i in range(1,len(collection)):

       #element to be compared
       cur_val = collection[i]

       #comparing the cur_val element with the sorted portion and swapping
       while i>0 and collection[i-1]>cur_val:
           collection[i] = collection[i-1]
           i = i-1
           collection[i] = cur_val

   return collection

Output :

[1, 2, 3, 3, 4, 5, 6]

Note : Please note that above program is compatible with Python 3 or higher version

Please check out other practical related to Data structures

2 thoughts on “Python program for Insertion sort”

Leave a Comment