1
1
/* *
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
5
4
*
6
- * \ details
5
+ * @ details
7
6
* 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].
11
25
*/
12
26
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
17
31
18
- /* * \namespace sorting
19
- * \brief Sorting algorithms
32
+ /* *
33
+ * @namespace sorting
34
+ * @brief Contains sorting algorithms
20
35
*/
21
36
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
28
44
*/
29
45
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;
48
55
}
49
- arr[j + 1 ] = last;
50
56
}
51
57
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
58
63
*/
59
64
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;
76
76
}
77
- arr->at (j + 1 ) = last;
78
77
}
79
78
80
79
} // namespace sorting
81
80
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
88
87
*/
89
88
template <typename T>
90
89
static void create_random_array (T *arr, int N) {
@@ -95,56 +94,58 @@ static void create_random_array(T *arr, int N) {
95
94
}
96
95
97
96
/* *
98
- * \ brief Test cases to validate the recursive insertion sort algorithm
97
+ * @ brief Test Cases for the sorting algorithm
99
98
*/
100
99
void tests () {
101
100
int arr1[10 ] = {78 , 34 , 35 , 6 , 34 , 56 , 3 , 56 , 2 , 4 };
102
101
std::cout << " Test 1... " ;
103
- sorting::recursiveInsertionSort (arr1, 10 );
102
+ sorting::insertionSort (arr1, 10 );
104
103
assert (std::is_sorted (arr1, arr1 + 10 ));
105
104
std::cout << " passed" << std::endl;
106
105
107
106
int arr2[5 ] = {5 , -3 , 7 , -2 , 1 };
108
107
std::cout << " Test 2... " ;
109
- sorting::recursiveInsertionSort (arr2, 5 );
108
+ sorting::insertionSort (arr2, 5 );
110
109
assert (std::is_sorted (arr2, arr2 + 5 ));
111
110
std::cout << " passed" << std::endl;
112
111
113
112
float arr3[5 ] = {5.6 , -3.1 , -3.0 , -2.1 , 1.8 };
114
113
std::cout << " Test 3... " ;
115
- sorting::recursiveInsertionSort (arr3, 5 );
114
+ sorting::insertionSort (arr3, 5 );
116
115
assert (std::is_sorted (arr3, arr3 + 5 ));
117
116
std::cout << " passed" << std::endl;
118
117
119
118
std::vector<float > arr4 ({5.6 , -3.1 , -3.0 , -2.1 , 1.8 });
120
119
std::cout << " Test 4... " ;
121
- sorting::recursiveInsertionSort (&arr4, arr4. size () );
120
+ sorting::insertionSort (&arr4);
122
121
assert (std::is_sorted (std::begin (arr4), std::end (arr4)));
123
122
std::cout << " passed" << std::endl;
124
123
125
124
int arr5[50 ];
126
125
std::cout << " Test 5... " ;
127
126
create_random_array (arr5, 50 );
128
- sorting::recursiveInsertionSort (arr5, 50 );
127
+ sorting::insertionSort (arr5, 50 );
129
128
assert (std::is_sorted (arr5, arr5 + 50 ));
130
129
std::cout << " passed" << std::endl;
131
130
132
131
float arr6[50 ];
133
132
std::cout << " Test 6... " ;
134
133
create_random_array (arr6, 50 );
135
- sorting::recursiveInsertionSort (arr6, 50 );
134
+ sorting::insertionSort (arr6, 50 );
136
135
assert (std::is_sorted (arr6, arr6 + 50 ));
137
136
std::cout << " passed" << std::endl;
138
137
}
139
138
140
139
/* *
141
- * \ brief Main function
140
+ * @ brief Main function
142
141
*
143
142
* Empty except for the call to `tests()`.
144
143
*
145
144
* @return 0 Always returns 0.
146
145
*/
147
146
int main () {
147
+ // / Running predefined tests to test the algorithm
148
148
tests ();
149
+
149
150
return 0 ;
150
151
}
0 commit comments