Open In App

C++ Program for Recursive Insertion Sort

Insertion sort is a simple and efficient sorting algorithm that is often used for small lists or as a building block for more complex algorithms. It works by dividing the input list into two parts: a sorted sublist of items that has already been traversed, and an unsorted sublist of items remaining to be sorted.

The algorithm begins with the second element of the list, and iterates through the remaining elements, one by one. For each element, it compares it to the previous elements in the sorted sublist, and inserts it into the correct position in the sublist by shifting the larger elements to the right.

Here is a step-by-step explanation of the iterative insertion sort algorithm:

  1.  The algorithm starts by assuming that the first element of the list is already sorted. Therefore, it begins the loop at the second element (i = 1).
  2.  For each element arr[i], the algorithm compares it to the elements in the sorted sublist arr[0…i-1]. It starts from the end of the sorted sublist and moves backwards until it finds the correct position to insert arr[i].
  3.  Once it finds the correct position, the algorithm shifts all larger elements to the right by one position, to make room for the insertion of arr[i]. This is done by copying each element arr[j] to arr[j+1], starting from the end of the sorted sublist and moving backwards until the correct position is reached.
  4.  Finally, the algorithm inserts arr[i] into the correct position in the sorted sublist, which is now arr[0…i].
  5.  The algorithm continues the loop until it has processed all elements in the list.

At the end of the loop, the input list is sorted in ascending order. The time complexity of the algorithm is O(n^2) in the worst case, but it has a best-case time complexity of O(n) for already sorted or nearly sorted lists.

Algorithm

// Sort an arr[] of size n
insertionSort(arr, n) 
    Loop from i = 1 to n-1.
       a) Pick element arr[i] and insert
          it into sorted sequence arr[0..i-1] 




// Recursive C++ program for insertion sort
#include <iostream>
using namespace std;
 
// Recursive function to sort an array using
// insertion sort
void insertionSortRecursive(int arr[], int n)
{
 // Base case
 if (n <= 1)
  return;
 
 // Sort first n-1 elements
 insertionSortRecursive( arr, n-1 );
 
 // Insert last element at its correct position
 // in sorted array.
 int last = arr[n-1];
 int j = n-2;
 
 /* Move elements of arr[0..i-1], that are
 greater than key, to one position ahead
 of their current position */
 while (j >= 0 && arr[j] > last)
 {
  arr[j+1] = arr[j];
  j--;
 }
 arr[j+1] = last;
}
 
// A utility function to print an array of size n
void printArray(int arr[], int n)
{
 for (int i=0; i < n; i++)
  cout << arr[i] <<" ";
}
 
/* Driver program to test insertion sort */
int main()
{
 int arr[] = {12, 11, 13, 5, 6};
 int n = sizeof(arr)/sizeof(arr[0]);
 
 insertionSortRecursive(arr, n);
 printArray(arr, n);
 
 return 0;
}

Time Complexity: O(n2)
Auxiliary Space: O(n)

Please refer complete article on Recursive Insertion Sort for more details!


Article Tags :