insertion sort.





This algorithm is an insertion sort algorithm, this is comparison based sorting algorithm,This algorithm compares the first two values,insertion sort is working similar way as we sort cards in our hand in a card game, similar to the card game,we assume the first card is already sorted then, we select unsorted card,if unsorted card is not greater then the card in hand , it is replace on the right side, otherwise left side,in the same way,Similarly, other unsold cards are moved to the right,
A similar approach is used in entry sort,and image is give good example,


code:

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

arr=[9,6,23,65,1,9,0,-1,31];
Insertionsort(arr);
for i in range(len(arr)):
    print(arr);
    if i==0:
        break;