Insertion Sort in Python

Insertion Sort is a sorting algorithm that sorts a list of items by picking up each item from the list, and placing it in its correct position, until the entire list is sorted.

In the program shown below, the integers are sorted in ascending order, using the Insertion Sort Algorithm. It has a worst case complexity of O(N2).

num = int(input("Number of integers to be stored: "))
myList = []
for i in range(0, num):
    item = int(input("Enter item " + str(i + 1) + ": "))
print("Original List:")
for i in range(1, len(myList)):
    key = myList[i]
    j = i - 1
    while j >= 0 and key < myList[j]:
        myList[j + 1] = myList[j]
        j = j - 1
        myList[j + 1] = key
print("Sorted List:")

3 thoughts on “Insertion Sort in Python

  1. An autobiographical number is a number such that the first digit of it counts how many zeroes are there in it, the second digit counts how many ones are there and so on.For example, 1210 has 1 zero, 2 ones, 1 two and 0 threes.Given N as the number of digits(N>0) ,WAP in Java to find all the automorphic numbers whose number of digits is N

  2. Sir,a program is given which states that:
    WAP in Java to accept a number and check whether it is Ore number or not.
    Ore numbers are positive integers whose divisors have a integer harmonic value. Ore numbers are often called as harmonic divisor number.
    For example, 6 has four divisors namely 1, 2, 3 and 6.
    Harmonic mean of the divisors is:

    The harmonic mean of divisors of 6 is 2, an integer. So, 6 is an Ore number or harmonic divisor number.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.