Write a python program to perform Insertion sort on defined list.

Tech Programmer
0

WRITE A PYTHON PROGRAM TO PERFORM INSERTION SORT ON DEFINED LIST AND PRINT SORTED AND UNSORTED LIST.


CODE FOR THE ABOVE  PYTHON  PROGRAM :


def insertion_sort(List):

    for i in range(1len(List)):
        key = List[i]
        j = i - 1
                
        while j >= 0 and key < List[j]:
            List[j + 1] = List[j]
            j = j - 1
            
        List[j + 1] = key

List = [9876543]
print("\n")
print("Unsorted List:"List)
insertion_sort(List)
print('Sorted List: 'List)
print("\n")


WRITTEN CODE FOR THE ABOVE PROGRAM :


def insertion_sort(List):


    for i in range(1, len(List)):

        key = List[i]

        j = i - 1

                

        while j >= 0 and key < List[j]:

            List[j + 1] = List[j]

            j = j - 1

            

        List[j + 1] = key


List = [9, 8, 7, 6, 5, 4, 3]

print("\n")

print("Unsorted List:", List)

insertion_sort(List)

print('Sorted List: ', List)

print("\n")


OUTPUT :




Post a Comment

0Comments
Post a Comment (0)

Featured Highlighted Categories

You'll discover all of the most up-to-date bring innovative here.

Python List theory with List Programs
Get complete python lists theory and list basic and insane programs for your practice.
CBSE SAMPLE PAPERS
Get CBSE sample and question papers of last 7 years with answers to get best practice for your Board Exams
Python String theory and String Programs
Python strings complete theory with illustrations and practice programs for free