Tuesday, 13 November 2018

11-C 29 Write a algorithm and python program sorting the numbers using insertion through function

                                                                                  Graphics Click Here


def ins_sort(n):                                                         
    alist=[]
    for i in range(n):
        num=int(input("Enter the number"))
        alist.append(num)
   
    print("Original list is:",alist)
 
    for i in range (1,len(alist)):
        key=alist[i]
        j=i-1
        while j>=0 and key < alist[j]:
            alist[j+1]=alist[j]
            j=j-1
        else:
            alist[j+1]=key
        print("step=",i,"list=",alist)
     
    print("list after sorting using insertion sort:",alist)

#main program
n=int(input("Enter the size of insertion sort"))
ins_sort(n)


>>> 
= RESTART: C:/Users/Student/AppData/Local/Programs/Python/Python37-32/ins.py =

Input
Enter the size of insertion sort 5
Enter the number 6
Enter the number 5
Enter the number 4
Enter the number 3
Enter the number 1
Original list is: [6, 5, 4, 3, 1]

Output
steps= 1 list= [5, 6, 4, 3, 1]
steps= 2 list= [4, 5, 6, 3, 1]
steps= 3 list= [3, 4, 5, 6, 1]
steps= 4 list= [1, 3, 4, 5, 6]
list after sorting using insertion sort: [1, 3, 4, 5, 6]
>>> 



No comments:

Post a Comment