Skip to content

Commit a3834f7

Browse files
authored
added recursive insertion sort algorithm
1 parent 0ecb6bd commit a3834f7

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

insertion_sort_recursive.cpp

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/**
2+
* \file
3+
* \brief [Recursive Insertion Sort Algorithm
4+
* (Insertion Sort)](https://en.wikipedia.org/wiki/Insertion_sort)
5+
*
6+
* \details
7+
* Insertion sort is a simple sorting algorithm that builds the final
8+
* sorted array one at a time. The recursive version applies the same
9+
* logic but sorts the sub-array recursively before inserting the current
10+
* element in its correct position.
11+
*/
12+
13+
#include <algorithm>
14+
#include <cassert>
15+
#include <iostream>
16+
#include <vector>
17+
18+
/** \namespace sorting
19+
* \brief Sorting algorithms
20+
*/
21+
namespace sorting {
22+
/** \brief
23+
* Recursive Insertion Sort Function
24+
*
25+
* @tparam T type of array
26+
* @param [in,out] arr Array to be sorted
27+
* @param n Size of Array
28+
*/
29+
template <typename T>
30+
void recursiveInsertionSort(T *arr, int n) {
31+
// Base case: Array of size 1 is already sorted
32+
if (n <= 1) {
33+
return;
34+
}
35+
36+
// Sort the first n-1 elements recursively
37+
recursiveInsertionSort(arr, n - 1);
38+
39+
// Insert the nth element at its correct position in the sorted array
40+
T last = arr[n - 1];
41+
int j = n - 2;
42+
43+
// Move elements of arr[0..n-1] that are greater than last to one
44+
// position ahead of their current position
45+
while (j >= 0 && arr[j] > last) {
46+
arr[j + 1] = arr[j];
47+
j--;
48+
}
49+
arr[j + 1] = last;
50+
}
51+
52+
/** Recursive Insertion Sort Function for a vector
53+
*
54+
* @tparam T type of vector elements
55+
* @param [in,out] arr pointer to vector to be sorted
56+
*/
57+
template <typename T>
58+
void recursiveInsertionSort(std::vector<T> *arr, size_t n) {
59+
// Base case: If the vector has one or fewer elements, it's already sorted
60+
if (n <= 1) {
61+
return;
62+
}
63+
64+
// Sort the first n-1 elements recursively
65+
recursiveInsertionSort(arr, n - 1);
66+
67+
// Insert the nth element at its correct position in the sorted vector
68+
T last = arr->at(n - 1);
69+
int j = n - 2;
70+
71+
while (j >= 0 && arr->at(j) > last) {
72+
arr->at(j + 1) = arr->at(j);
73+
j--;
74+
}
75+
arr->at(j + 1) = last;
76+
}
77+
78+
} // namespace sorting
79+
80+
/** Test Cases to test recursive algorithm */
81+
void tests() {
82+
int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4};
83+
std::cout << "Test 1... ";
84+
sorting::recursiveInsertionSort(arr1, 10);
85+
assert(std::is_sorted(arr1, arr1 + 10));
86+
std::cout << "passed" << std::endl;
87+
88+
int arr2[5] = {5, -3, 7, -2, 1};
89+
std::cout << "Test 2... ";
90+
sorting::recursiveInsertionSort(arr2, 5);
91+
assert(std::is_sorted(arr2, arr2 + 5));
92+
std::cout << "passed" << std::endl;
93+
94+
float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8};
95+
std::cout << "Test 3... ";
96+
sorting::recursiveInsertionSort(arr3, 5);
97+
assert(std::is_sorted(arr3, arr3 + 5));
98+
std::cout << "passed" << std::endl;
99+
100+
std::vector<float> arr4({5.6, -3.1, -3.0, -2.1, 1.8});
101+
std::cout << "Test 4... ";
102+
sorting::recursiveInsertionSort(&arr4, arr4.size());
103+
assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
104+
std::cout << "passed" << std::endl;
105+
106+
int arr5[50];
107+
std::cout << "Test 5... ";
108+
create_random_array(arr5, 50);
109+
sorting::recursiveInsertionSort(arr5, 50);
110+
assert(std::is_sorted(arr5, arr5 + 50));
111+
std::cout << "passed" << std::endl;
112+
113+
float arr6[50];
114+
std::cout << "Test 6... ";
115+
create_random_array(arr6, 50);
116+
sorting::recursiveInsertionSort(arr6, 50);
117+
assert(std::is_sorted(arr6, arr6 + 50));
118+
std::cout << "passed" << std::endl;
119+
}
120+
121+
/** Main Function */
122+
int main() {
123+
/// Running predefined tests to test recursive algorithm
124+
tests();
125+
126+
/// For user interaction
127+
size_t n;
128+
std::cout << "Enter the length of your array (0 to exit): ";
129+
std::cin >> n;
130+
if (n == 0) {
131+
return 0;
132+
}
133+
134+
int *arr = new int[n];
135+
std::cout << "Enter any " << n << " Numbers for Unsorted Array : ";
136+
137+
for (int i = 0; i < n; i++) {
138+
std::cin >> arr[i];
139+
}
140+
141+
sorting::recursiveInsertionSort(arr, n);
142+
143+
std::cout << "\nSorted Array : ";
144+
for (int i = 0; i < n; i++) {
145+
std::cout << arr[i] << " ";
146+
}
147+
148+
std::cout << std::endl;
149+
delete[] arr;
150+
return 0;
151+
}
152+

0 commit comments

Comments
 (0)