Insertion Sort in DSA using Python

Program 1

# Implementation of Insertion sort
import os 
mylist=[]
os.system('cls')
n=int(input("Enter the limit: "))
for i in range(n):
    x=int(input())
    mylist.append(x)

# Insertion sort
for k in range(1,n):
    temp=mylist[k]
    j=k-1
    while(temp<mylist[j] and j>=0):
        mylist[j+1]=mylist[j]
        j=j-1
    mylist[j+1]=temp      

print("------Sorted Elements------------")

print(mylist)

 

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *