C++ Program Recursive Insertion Sort

Insertion Sort is one of the sorting algorithms used to sort data by inserting elements like a deck of cards. All the elements are arranged from left to right then considering the first one as already sorted, insert rest to the sorted list on the left. Each element is compared with each element in
the left list until it is inserted at its correct position.
int arr[5]= { 5,4,2,1,3 };
int arr[5]= { 5,4,2,1,3 };
int i, j ;
int i, j ;
Traverse from index j=i+1 to j<array size
Traverse from index j=i+1 to j<array size
For each element arr[j] compare it with elements in list arr[0 to i] till element is found such that arr[i]<arr[j] and arr[i+1]>=arr[j].
For each element arr[j] compare it with elements in list arr[0 to i] till element is found such that arr[i]<arr[j] and arr[i+1]>=arr[j].
Place arr[j] in the list and move all greater elements one position right.
Place arr[j] in the list and move all greater elements one position right.
End
End
If array length is 1 then return.
If array length is 1 then return.
Recursively sor elements at index 0 to array size-1
Recursively sor elements at index 0 to array size-1
Insert last element in the sorted array
Insert last element in the sorted array
Input − Arr[] = { 5,7,2,3,1,4 }; length=6
Output − Sorted array: 1 2 3 4 5 7
Explanation −
5 7 2 3 1 4 → 5 already sorted
5 7 2 3 1 4 → 7 at correct place
2 5 7 3 1 4 → 2 compared with 5,7 and inserted
2 3 5 7 1 4 → 3 compared with 5,7 and inserted
1 2 3 5 7 4 → 1 compared with 2,3,5,7 and inserted
1 2 3 4 5 7 → 4 compared with 5,7 and inserted
Input − Arr[] = { 1, 2, 3, 3, 2 };
Output − Sorted array: 1 2 2 3 3
Explanation −
1, 2, 3, 3, 2 → 1 already sorted
1, 2, 3, 3, 2 → 2 at correct place
1, 2, 3, 3, 2 → 3 at correct place
1, 2, 3, 3, 2 → 3 at correct place
1, 2, 2, 3, 3 → 2 compared with 3,3 and inserted
In the recursive approach of Bubble sort, the base case is array length = 1. Otherwise traverse the array using single for loop and swap elements accordingly.
Take input array Arr[] and length as number of elements in it.
Take input array Arr[] and length as number of elements in it.
Function recurbublSort(int arr[], int len) takes the array and its length and sorts the array recursively using bubble sort.
Function recurbublSort(int arr[], int len) takes the array and its length and sorts the array recursively using bubble sort.
Take a variable temp.
Take a variable temp.
If array length is 1 then return void.
If array length is 1 then return void.
Else traverse the array using single for loop and for each element arr[i]>arr[i+1], swap those elements.
Else traverse the array using single for loop and for each element arr[i]>arr[i+1], swap those elements.
Set temp=arr[i], arr[i]=arr[i+1] and arr[i+1]=temp.
Set temp=arr[i], arr[i]=arr[i+1] and arr[i+1]=temp.
Now decrement length by 1 as the previous loop placed the largest element at the last position.
Now decrement length by 1 as the previous loop placed the largest element at the last position.
Do recursive call to recurbublSort(arr,len).
Do recursive call to recurbublSort(arr,len).
At the end of all calls, when len becomes 1 we will come out of recursion and the array will be sorted.
At the end of all calls, when len becomes 1 we will come out of recursion and the array will be sorted.
Print the sorted array inside main.
Print the sorted array inside main.
#include <bits/stdc++.h>
using namespace std;
void recurbublSort(int arr[], int len){
   int temp;

   if (len == 1){
      return;
   }
   for (int i=0; i<len-1; i++){
      if (arr[i] > arr[i+1]){
         temp=arr[i];
         arr[i]=arr[i+1];
         arr[i+1]=temp;
      }
   }
   len=len-1;
   recurbublSort(arr, len);
}
int main(){
   int Arr[] = {21, 34, 20, 31, 78, 43, 66};
   int length = sizeof(Arr)/sizeof(Arr[0]);

   recurbublSort(Arr, length);

   cout<<"Sorted array : ";
   for(int i=0;i<length;i++){
      cout<<Arr[i]<<" ";
   }

   return 0;
}
If we run the above code it will generate the following Output
Sorted array : 20 21 31 34 43 66 78