Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

Program to perform Selection and insertion sort in python


" Program to perform Selection and insertion sort"
print ("Program to perform Selection and insertion sort")
print ("""Enter Your Choice
        1. Selection Sort
        2. Inseretion Sort
        3. Exit""")
#FUNCTION TO PERFORM Selection Sort
def S_sort():
    print ("Selection Search")
#************Code to get the values************
    List_size=int(input("Enter the Number of Elements : "))
    Slist=[]
    for i in range(0,List_size):
        print ("Enter the ", i ," item   ")
        E_item=int(input("The Value : "))
        Slist.append(E_item)
    print ("The Unsorted List")
    print (Slist)
#*************Code to perform selection sort
    for i in range( len(Slist)-1,0,-1 ):
       MaxValueIndex = 0
       for k in range( 1, i+1 ):
           if Slist[k] < Slist[MaxValueIndex]:
               MaxValueIndex = k
           Slist[i], Slist[MaxValueIndex]= Slist[MaxValueIndex],Slist[i]
    print ("The Result after performing Selection Sort")
    print (Slist)

   
       
#FUNCTION TO PERFORM  Insertion sort
def I_sort():
    print ("Insertion Search")
    #************Code to get the values************
    List_size=int(input("Enter the Number of Elements : "))
    Ilist=[]
    for i in range(0,List_size):
        print ("Enter the ", i ," item   ")
        E_item=int(input("The Value : "))
        Ilist.append(E_item)
    print ("The Unsorted List")
    print (Ilist)
#*************Code to perform Insertion sort
    for i in range(1, len(Ilist)):
        j = i
        while j > 0 and Ilist[j - 1] < Ilist[j]:
            Ilist[j - 1], Ilist[j] = Ilist[j], Ilist[j - 1]
            j -= 1
    print ("The Result after performing Insertion Sort")
    print (Ilist)

 
def close():
    quit()

option = {1: S_sort,
          2: I_sort,
          3: close,}

#MAIN FUNCTION
def main():
    op=int(input("Choice Value: "))
    option[op]()
t=1

while t==1:
    main ()

No comments:

Post a Comment

Flag Counter