Skip to content

Commit ab76cac

Browse files
authored
Updated documentation and structure in insertion_sort_recursive.cpp
1 parent 330cd9d commit ab76cac

File tree

1 file changed

+76
-75
lines changed

1 file changed

+76
-75
lines changed
Lines changed: 76 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,89 @@
11
/**
2-
* \file
3-
* \brief [Recursive Insertion Sort Algorithm
4-
* (Insertion Sort)](https://en.wikipedia.org/wiki/Insertion_sort)
2+
* @file
3+
* @brief Insertion Sort Algorithm
54
*
6-
* \details
5+
* @details
76
* 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.
7+
* sorted array one element at a time. It is much less efficient compared
8+
* to other sorting algorithms like heap sort, merge sort, or quick sort.
9+
*
10+
* However, it has several advantages:
11+
* - Easy to implement.
12+
* - Efficient for small data sets.
13+
* - More efficient than other O(n²) algorithms like selection sort or bubble sort.
14+
* - Stable: it does not change the relative order of elements with equal keys.
15+
*
16+
* Insertion sort works similarly to how people sort playing cards in their hands.
17+
* The algorithm iterates through the list and inserts each element into its correct
18+
* position in the sorted portion of the array.
19+
*
20+
* Example execution:
21+
* 1. Start with the array [4, 3, 2, 5, 1].
22+
* 2. Insert 3 in its correct position: [3, 4, 2, 5, 1].
23+
* 3. Insert 2: [2, 3, 4, 5, 1].
24+
* 4. Continue this until the array is sorted: [1, 2, 3, 4, 5].
1125
*/
1226

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
27+
#include <algorithm> ///< for std::is_sorted
28+
#include <cassert> ///< for assert function in testing
29+
#include <iostream> ///< for std::cout and std::endl
30+
#include <vector> ///< for using std::vector
1731

18-
/** \namespace sorting
19-
* \brief Sorting algorithms
32+
/**
33+
* @namespace sorting
34+
* @brief Contains sorting algorithms
2035
*/
2136
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
37+
38+
/**
39+
* @brief Insertion Sort Function
40+
*
41+
* @tparam T Type of the array elements
42+
* @param [in,out] arr Array to be sorted
43+
* @param n Size of the array
2844
*/
2945
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--;
46+
void insertionSort(T *arr, int n) {
47+
for (int i = 1; i < n; i++) {
48+
T temp = arr[i];
49+
int j = i - 1;
50+
while (j >= 0 && temp < arr[j]) {
51+
arr[j + 1] = arr[j];
52+
j--;
53+
}
54+
arr[j + 1] = temp;
4855
}
49-
arr[j + 1] = last;
5056
}
5157

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+
* @brief Insertion Sort for a vector
60+
*
61+
* @tparam T Type of the vector elements
62+
* @param [in,out] arr Pointer to the vector to be sorted
5863
*/
5964
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--;
65+
void insertionSort(std::vector<T> *arr) {
66+
size_t n = arr->size();
67+
68+
for (size_t i = 1; i < n; i++) {
69+
T temp = arr->at(i);
70+
int32_t j = i - 1;
71+
while (j >= 0 && temp < arr->at(j)) {
72+
arr->at(j + 1) = arr->at(j);
73+
j--;
74+
}
75+
arr->at(j + 1) = temp;
7676
}
77-
arr->at(j + 1) = last;
7877
}
7978

8079
} // namespace sorting
8180

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
81+
/**
82+
* @brief Helper function to create a random array
83+
*
84+
* @tparam T Type of the array elements
85+
* @param arr Array to fill (must be pre-allocated)
86+
* @param N Number of elements in the array
8887
*/
8988
template <typename T>
9089
static void create_random_array(T *arr, int N) {
@@ -95,56 +94,58 @@ static void create_random_array(T *arr, int N) {
9594
}
9695

9796
/**
98-
* \brief Test cases to validate the recursive insertion sort algorithm
97+
* @brief Test Cases for the sorting algorithm
9998
*/
10099
void tests() {
101100
int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4};
102101
std::cout << "Test 1... ";
103-
sorting::recursiveInsertionSort(arr1, 10);
102+
sorting::insertionSort(arr1, 10);
104103
assert(std::is_sorted(arr1, arr1 + 10));
105104
std::cout << "passed" << std::endl;
106105

107106
int arr2[5] = {5, -3, 7, -2, 1};
108107
std::cout << "Test 2... ";
109-
sorting::recursiveInsertionSort(arr2, 5);
108+
sorting::insertionSort(arr2, 5);
110109
assert(std::is_sorted(arr2, arr2 + 5));
111110
std::cout << "passed" << std::endl;
112111

113112
float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8};
114113
std::cout << "Test 3... ";
115-
sorting::recursiveInsertionSort(arr3, 5);
114+
sorting::insertionSort(arr3, 5);
116115
assert(std::is_sorted(arr3, arr3 + 5));
117116
std::cout << "passed" << std::endl;
118117

119118
std::vector<float> arr4({5.6, -3.1, -3.0, -2.1, 1.8});
120119
std::cout << "Test 4... ";
121-
sorting::recursiveInsertionSort(&arr4, arr4.size());
120+
sorting::insertionSort(&arr4);
122121
assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
123122
std::cout << "passed" << std::endl;
124123

125124
int arr5[50];
126125
std::cout << "Test 5... ";
127126
create_random_array(arr5, 50);
128-
sorting::recursiveInsertionSort(arr5, 50);
127+
sorting::insertionSort(arr5, 50);
129128
assert(std::is_sorted(arr5, arr5 + 50));
130129
std::cout << "passed" << std::endl;
131130

132131
float arr6[50];
133132
std::cout << "Test 6... ";
134133
create_random_array(arr6, 50);
135-
sorting::recursiveInsertionSort(arr6, 50);
134+
sorting::insertionSort(arr6, 50);
136135
assert(std::is_sorted(arr6, arr6 + 50));
137136
std::cout << "passed" << std::endl;
138137
}
139138

140139
/**
141-
* \brief Main function
140+
* @brief Main function
142141
*
143142
* Empty except for the call to `tests()`.
144143
*
145144
* @return 0 Always returns 0.
146145
*/
147146
int main() {
147+
/// Running predefined tests to test the algorithm
148148
tests();
149+
149150
return 0;
150151
}

0 commit comments

Comments
 (0)