Skip to content
Related Articles

Related Articles

C++ Program for Recursive Insertion Sort

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 13 Jun, 2022
View Discussion
Improve Article
Save Article

Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands.Below is an iterative algorithm for insertion sort


// 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)
 // 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];
 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!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!