0% found this document useful (0 votes)
6 views1 page

Insertion Sort Implementation in Python

The document contains a Python implementation of the Insertion Sort algorithm. It defines a function that sorts a list of integers and prints the number of shifts made during the sorting process. The user is prompted to input the number of elements and the elements themselves, which are then sorted using the defined function.

Uploaded by

FUTURE ENGINEERS
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Insertion Sort Implementation in Python

The document contains a Python implementation of the Insertion Sort algorithm. It defines a function that sorts a list of integers and prints the number of shifts made during the sorting process. The user is prompted to input the number of elements and the elements themselves, which are then sorted using the defined function.

Uploaded by

FUTURE ENGINEERS
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# -*- coding: utf-8 -*-

"""

Insertion Sort - CodeAbbey

Created on Sun Mar 1 18:49:30 2020

@author: Diego Sanchez


"""

def insertionSort(nlist):

for index in range(1,len(nlist)):


c = 0
currentvalue = nlist[index]
position = index
while position>0 and nlist[position-1]>currentvalue:
nlist[position]=nlist[position-1]
c += 1
position = position-1
nlist[position]=currentvalue
print(c)

#nlist = [14,46,43,27,57,41,45,21,70]
nlist = []
n = int(input())
for i in range(n):
l = int(input())
[Link](l)

insertionSort(nlist)
print(nlist)

You might also like