VOOZH about

URL: https://www.geeksforgeeks.org/dsa/recursive-insertion-sort/

⇱ Recursive Insertion Sort - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Recursive Insertion Sort

Last Updated : 23 Jul, 2025

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
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] 


Example: 
 

πŸ‘ insertion-sort


Refer Insertion Sort for more details.
How to implement it recursively? 
Recursive Insertion Sort has no performance/implementation advantages, but can be a good question to check one’s understanding of Insertion Sort and recursion.
If we take a closer look at Insertion Sort algorithm, we keep processed elements sorted and insert new elements one by one in the sorted array.
Recursion Idea. 
 

  1. Base Case: If array size is 1 or smaller, return.
  2. Recursively sort first n-1 elements.
  3. Insert last element at its correct position in sorted array.


Below is implementation of above idea.
 

Output : 

5 6 11 12 13 

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


 


 


about the topic discussed above
 

Comment
Article Tags:
Article Tags: