Skip to content

Commit 330cd9d

Browse files
authored
Created insertion_sort_recursive.cpp file
1 parent 2acae01 commit 330cd9d

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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> // for std::is_sorted and std::swap
14+
#include <cassert> // for assert
15+
#include <iostream> // for std::cout and std::endl
16+
#include <vector> // for std::vector
17+
18+
/** \namespace sorting
19+
* \brief Sorting algorithms
20+
*/
21+
namespace sorting {
22+
/**
23+
* \brief Recursive Insertion Sort Function for an array
24+
*
25+
* @tparam T Type of the elements in the array
26+
* @param[in,out] arr Pointer to the array to be sorted
27+
* @param[in] n Size of the 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+
/**
53+
* \brief Recursive Insertion Sort Function for a vector
54+
*
55+
* @tparam T Type of the elements in the vector
56+
* @param[in,out] arr Pointer to the vector to be sorted
57+
* @param[in] n Size of the vector
58+
*/
59+
template <typename T>
60+
void recursiveInsertionSort(std::vector<T> *arr, size_t n) {
61+
// Base case: If the vector has one or fewer elements, it's already sorted
62+
if (n <= 1) {
63+
return;
64+
}
65+
66+
// Sort the first n-1 elements recursively
67+
recursiveInsertionSort(arr, n - 1);
68+
69+
// Insert the nth element at its correct position in the sorted vector
70+
T last = arr->at(n - 1);
71+
int j = n - 2;
72+
73+
while (j >= 0 && arr->at(j) > last) {
74+
arr->at(j + 1) = arr->at(j);
75+
j--;
76+
}
77+
arr->at(j + 1) = last;
78+
}
79+
80+
} // namespace sorting
81+
82+
/**
83+
* \brief Helper function to create a random array
84+
*
85+
* @tparam T Type of array elements
86+
* @param[out] arr Pointer to the array to be filled
87+
* @param[in] N Size of the array
88+
*/
89+
template <typename T>
90+
static void create_random_array(T *arr, int N) {
91+
while (N--) {
92+
double r = (std::rand() % 10000 - 5000) / 100.f;
93+
arr[N] = static_cast<T>(r);
94+
}
95+
}
96+
97+
/**
98+
* \brief Test cases to validate the recursive insertion sort algorithm
99+
*/
100+
void tests() {
101+
int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4};
102+
std::cout << "Test 1... ";
103+
sorting::recursiveInsertionSort(arr1, 10);
104+
assert(std::is_sorted(arr1, arr1 + 10));
105+
std::cout << "passed" << std::endl;
106+
107+
int arr2[5] = {5, -3, 7, -2, 1};
108+
std::cout << "Test 2... ";
109+
sorting::recursiveInsertionSort(arr2, 5);
110+
assert(std::is_sorted(arr2, arr2 + 5));
111+
std::cout << "passed" << std::endl;
112+
113+
float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8};
114+
std::cout << "Test 3... ";
115+
sorting::recursiveInsertionSort(arr3, 5);
116+
assert(std::is_sorted(arr3, arr3 + 5));
117+
std::cout << "passed" << std::endl;
118+
119+
std::vector<float> arr4({5.6, -3.1, -3.0, -2.1, 1.8});
120+
std::cout << "Test 4... ";
121+
sorting::recursiveInsertionSort(&arr4, arr4.size());
122+
assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
123+
std::cout << "passed" << std::endl;
124+
125+
int arr5[50];
126+
std::cout << "Test 5... ";
127+
create_random_array(arr5, 50);
128+
sorting::recursiveInsertionSort(arr5, 50);
129+
assert(std::is_sorted(arr5, arr5 + 50));
130+
std::cout << "passed" << std::endl;
131+
132+
float arr6[50];
133+
std::cout << "Test 6... ";
134+
create_random_array(arr6, 50);
135+
sorting::recursiveInsertionSort(arr6, 50);
136+
assert(std::is_sorted(arr6, arr6 + 50));
137+
std::cout << "passed" << std::endl;
138+
}
139+
140+
/**
141+
* \brief Main function
142+
*
143+
* Empty except for the call to `tests()`.
144+
*
145+
* @return 0 Always returns 0.
146+
*/
147+
int main() {
148+
tests();
149+
return 0;
150+
}

0 commit comments

Comments
 (0)