![]() |
VOOZH | about |
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:
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.
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