Insertion Sort

Jan. 15, 2022, 1:57 p.m.


Insertion sort is another O(N2) quadratic running time algorithm.
On large data set it is very inefficient but on array with 10-20 items it is quite good.
Stable sorting algorithm: which maintains the relative order of items with equal values.
In place algorithm : does not need any additional memory.

Insertion sort is another O(N2) quadratic running time algorithm.
On large data set it is very inefficient but on array with 10-20 items it is quite good.
Stable sorting algorithm: which maintains the relative order of items with equal values.
In place algorithm : does not need any additional memory.

def insertion_sort(my_list):
    for i in range(1,len(my_list)):
        temp = my_list[i]
        j=i-1
        while temp<my_list[j] and j>-1:
            my_list[j+1] = my_list[j]
            my_list[j]=temp
            j-=1
    return my_list



Tags


Comments