Insertion Sort in DSA Python

Program 1

# Program for Insertion sort
import os
os.system('cls')
mylist=[]
n=int(input("Enter the limit"))
print("Enter elements in collection")
for i in range(0,n):
    x=int(input())
    mylist.append(x)
    # Login for 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 element-------------")      
for i in range(0,n):
     print(mylist[i])

 

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 *