1
1
/* *
2
2
* @file
3
3
* @brief Insertion Sort Algorithm
4
- *
4
+ * @author [Dhanush S] (https://github.com/Fandroid745)
5
+ *
5
6
* @details
6
7
* Insertion sort is a simple sorting algorithm that builds the final
7
8
* sorted array one element at a time. It is much less efficient compared
16
17
* Insertion sort works similarly to how people sort playing cards in their hands.
17
18
* The algorithm iterates through the list and inserts each element into its correct
18
19
* position in the sorted portion of the array.
19
- *
20
+ *
21
+ * The time complexity of the algorithm is \f$O(n^2)\f$, and in some cases, it
22
+ * can be \f$O(n)\f$.
23
+ *
20
24
* Example execution:
21
25
* 1. Start with the array [4, 3, 2, 5, 1].
22
26
* 2. Insert 3 in its correct position: [3, 4, 2, 5, 1].
23
27
* 3. Insert 2: [2, 3, 4, 5, 1].
24
28
* 4. Continue this until the array is sorted: [1, 2, 3, 4, 5].
25
29
*/
26
30
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
31
+
32
+ #include < algorithm> // / for std::is_sorted
33
+ #include < cassert> // / for assert function in testing
34
+ #include < iostream> // / for std::cout and std::endl
35
+ #include < vector> // / for using std::vector
31
36
32
37
/* *
33
38
* @namespace sorting
@@ -39,7 +44,7 @@ namespace sorting {
39
44
* @brief Insertion Sort Function
40
45
*
41
46
* @tparam T Type of the array elements
42
- * @param [in,out] arr Array to be sorted
47
+ * @param[in,out] arr Array to be sorted
43
48
* @param n Size of the array
44
49
*/
45
50
template <typename T>
@@ -96,7 +101,7 @@ static void create_random_array(T *arr, int N) {
96
101
/* *
97
102
* @brief Test Cases for the sorting algorithm
98
103
*/
99
- void tests () {
104
+ static void tests () {
100
105
int arr1[10 ] = {78 , 34 , 35 , 6 , 34 , 56 , 3 , 56 , 2 , 4 };
101
106
std::cout << " Test 1... " ;
102
107
sorting::insertionSort (arr1, 10 );
@@ -141,11 +146,11 @@ void tests() {
141
146
*
142
147
* Empty except for the call to `tests()`.
143
148
*
144
- * @return 0 Always returns 0 .
149
+ * @return 0 on successful exit .
145
150
*/
146
151
int main () {
147
152
// / Running predefined tests to test the algorithm
148
- tests ();
153
+ tests (); // / run self test implementations
149
154
150
155
return 0 ;
151
156
}
0 commit comments