diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 6bc42b1e..4e5a699e 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -20,6 +20,7 @@ 1. [Factorial](recursion/factorial.py) 2. [n-th Fibonacci number](recursion/n-th_fibonacci_number.py) +3. [Recursive Insertion Sort](recursion/recursive_insertion_sort.py) ## Scheduling diff --git a/algorithms/Python/recursion/recursive_insertion_sort.py b/algorithms/Python/recursion/recursive_insertion_sort.py new file mode 100644 index 00000000..aabc0637 --- /dev/null +++ b/algorithms/Python/recursion/recursive_insertion_sort.py @@ -0,0 +1,30 @@ +""" +Base Case: If array size is 1 or smaller, return. +Recursively sort first n-1 elements. +Insert last element at its correct position in sorted array. +""" + +def insertionSort_rec(array, n): + # base case + if n <= 1: + return + # Sort first n-1 elements + insertionSort_rec(array, n-1) + '''Insert last element at its correct position + in sorted array.''' + end = array[n-1] + 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 and array[j] > end): + array[j+1] = array[j] + j -= 1 + array[j+1] = end + + +arr = [6, 5, 2, 7, 12, 9, 1, 4] +insertionSort_rec(arr, len(arr)) +print("Sorted array is:") +print(arr)