What should be the algorithm of the below statement?

The idea is to recursively sort the array from index 1 to n-1 and then insert the nth element at apt place.

Algorithm:

insertion_sort(Arr, n):
    if(n <= 1)
        return
    insertion_sort(Arr, n-1)
    temp = Arr[n]
    for (i = n; i > 0; i--):
        if(Arr[i] > temp):
            Arr[i+1] = Arr[i]
        else:
            break
    Arr[i+1] = temp

Leave a Comment