-5

Can anyone tell me the algorithm for this question? Q..We can express insertion sort as a recursive procedure as follows. In order to sort A[1..n], we recursively sort A[1..nāˆ’1] and then insert A[n] into the sorted array A[1..nāˆ’1]. Write a recurrence for the running time of this recursive version of insertion sort.

prabhupant
  • 31
  • 8
  • It's unclear what you're asking. Are you asking for a recursive insertion sort, or are you asking how to write the recurrence relation? – Jim Mischel Jul 13 '16 at 15:20

2 Answers2

0

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
Avikalp Srivastava
  • 139
  • 1
  • 1
  • 9
0
public static void RecursiveInsertionSort(int[] array, int number) {
        if (number >= 1)
            return;

        RecursiveInsertionSort(array, number - 1);

        int currentnumber = array[number];
        int i;
        for (i = number - 1; i >= 0;) {

            if (array[i] > currentnumber) {
                array[i + 1] = array[i];
                i--;
            } else {
                break;
            }

        }
        array[i + 1] = currentnumber;

    }