From 2f556034f8ab6f7efc70427bb6f60c8945937085 Mon Sep 17 00:00:00 2001 From: Shantanu675 Date: Sat, 11 Apr 2026 11:40:20 +0530 Subject: [PATCH 1/4] Recursive Insertion Sort Algo added --- .../sorts/RecursiveInsertionSort.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/main/java/com/thealgorithms/sorts/RecursiveInsertionSort.java diff --git a/src/main/java/com/thealgorithms/sorts/RecursiveInsertionSort.java b/src/main/java/com/thealgorithms/sorts/RecursiveInsertionSort.java new file mode 100644 index 000000000000..8f8ef5d15df2 --- /dev/null +++ b/src/main/java/com/thealgorithms/sorts/RecursiveInsertionSort.java @@ -0,0 +1,88 @@ +package com.thealgorithms.sorts; + +/** + * Recursive Insertion Sort algorithm. + * + * This is a recursive implementation of the standard Insertion Sort algorithm. + * Instead of iterating through the array, it sorts the first n-1 elements recursively + * and then inserts the nth element into its correct position. + * + * Concept: + * - Divide the problem into smaller subproblems by sorting first n-1 elements. + * - Insert the last element into the sorted portion. + * + * Time Complexity: + * - Best case: O(n) – array is already sorted + * - Average case: O(n^2) + * - Worst case: O(n^2) – array is reverse sorted + * + * Space Complexity: + * - O(n) – due to recursion stack + * + * Note: + * - This implementation is mainly useful for understanding recursion. + * - Iterative insertion sort is preferred in production due to lower space overhead. + * + * @see SortAlgorithm + */ +public class RecursiveInsertionSort implements SortAlgorithm { + + /** + * Sorts the given array using recursive insertion sort. + * + * @param array The array to be sorted + * @param The type of elements in the array, which must be comparable + * @return The sorted array + */ + @Override + public > T[] sort(T[] array) { + if (array == null || array.length <= 1) { + return array; + } + + recursiveSort(array, array.length); + return array; + } + + /** + * Recursively sorts the first n elements of the array. + * + * @param array The array to be sorted + * @param n The number of elements to sort + * @param The type of elements in the array + */ + private > void recursiveSort(T[] array, int n) { + + // Base case: single element is already sorted + if (n <= 1) { + return; + } + + // Recursively sort first n-1 elements + recursiveSort(array, n - 1); + + // Insert the nth element into the sorted subarray + insert(array, n); + } + + /** + * Inserts the nth element into its correct position in the sorted subarray [0...n-2]. + * + * @param array The array containing sorted subarray and one unsorted element + * @param n The size of the subarray to consider + * @param The type of elements in the array + */ + private > void insert(T[] array, int n) { + final T key = array[n - 1]; + int j = n - 2; + + // Shift elements greater than key to one position ahead + while (j >= 0 && SortUtils.less(key, array[j])) { + array[j + 1] = array[j]; + j--; + } + + // Place key at correct position + array[j + 1] = key; + } +} \ No newline at end of file From de50138d1573e58958f61d313516ca5d2eb568ed Mon Sep 17 00:00:00 2001 From: Shantanu675 Date: Sat, 11 Apr 2026 11:57:48 +0530 Subject: [PATCH 2/4] Recursive Insertion Sort Test Algo added --- .../sorts/RecursiveInsertionSortTest.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/test/java/com/thealgorithms/sorts/RecursiveInsertionSortTest.java diff --git a/src/test/java/com/thealgorithms/sorts/RecursiveInsertionSortTest.java b/src/test/java/com/thealgorithms/sorts/RecursiveInsertionSortTest.java new file mode 100644 index 000000000000..d3e06a10d9f9 --- /dev/null +++ b/src/test/java/com/thealgorithms/sorts/RecursiveInsertionSortTest.java @@ -0,0 +1,59 @@ +package com.thealgorithms.sorts; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.api.Test; + +class RecursiveInsertionSortTest { + + private final RecursiveInsertionSort sorter = new RecursiveInsertionSort(); + + @Test + void testEmptyArray() { + Integer[] input = {}; + Integer[] expected = {}; + assertArrayEquals(expected, sorter.sort(input)); + } + + @Test + void testSingleElement() { + Integer[] input = {1}; + Integer[] expected = {1}; + assertArrayEquals(expected, sorter.sort(input)); + } + + @Test + void testAlreadySorted() { + Integer[] input = {1, 2, 3, 4, 5}; + Integer[] expected = {1, 2, 3, 4, 5}; + assertArrayEquals(expected, sorter.sort(input)); + } + + @Test + void testReverseSorted() { + Integer[] input = {5, 4, 3, 2, 1}; + Integer[] expected = {1, 2, 3, 4, 5}; + assertArrayEquals(expected, sorter.sort(input)); + } + + @Test + void testRandomOrder() { + Integer[] input = {3, 1, 4, 5, 2}; + Integer[] expected = {1, 2, 3, 4, 5}; + assertArrayEquals(expected, sorter.sort(input)); + } + + @Test + void testDuplicates() { + Integer[] input = {4, 2, 5, 2, 3}; + Integer[] expected = {2, 2, 3, 4, 5}; + assertArrayEquals(expected, sorter.sort(input)); + } + + @Test + void testStrings() { + String[] input = {"banana", "apple", "cherry"}; + String[] expected = {"apple", "banana", "cherry"}; + assertArrayEquals(expected, sorter.sort(input)); + } +} \ No newline at end of file From 26670a6869c422389c4faf0f0195dba3ed4cde8f Mon Sep 17 00:00:00 2001 From: Shantanu675 Date: Sat, 11 Apr 2026 12:38:48 +0530 Subject: [PATCH 3/4] solve clang-format --- .../java/com/thealgorithms/sorts/RecursiveInsertionSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/sorts/RecursiveInsertionSort.java b/src/main/java/com/thealgorithms/sorts/RecursiveInsertionSort.java index 8f8ef5d15df2..ef906d5ca7de 100644 --- a/src/main/java/com/thealgorithms/sorts/RecursiveInsertionSort.java +++ b/src/main/java/com/thealgorithms/sorts/RecursiveInsertionSort.java @@ -85,4 +85,4 @@ private > void insert(T[] array, int n) { // Place key at correct position array[j + 1] = key; } -} \ No newline at end of file +} From 3d83beb7b1e2424a7201ec4e8ba77062c65cbadb Mon Sep 17 00:00:00 2001 From: Shantanu675 Date: Sat, 11 Apr 2026 18:14:38 +0530 Subject: [PATCH 4/4] solve clang-format in test file also --- .../com/thealgorithms/sorts/RecursiveInsertionSortTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/sorts/RecursiveInsertionSortTest.java b/src/test/java/com/thealgorithms/sorts/RecursiveInsertionSortTest.java index d3e06a10d9f9..532b7ea426b5 100644 --- a/src/test/java/com/thealgorithms/sorts/RecursiveInsertionSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/RecursiveInsertionSortTest.java @@ -56,4 +56,4 @@ void testStrings() { String[] expected = {"apple", "banana", "cherry"}; assertArrayEquals(expected, sorter.sort(input)); } -} \ No newline at end of file +}