Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit faff217

Browse files
feat: add recursive inserstion sort (TheAlgorithms#2764)
* added recursive insertion sort algorithm * Modified the main function and removed user interaction as requested * Deleted previous file * Updated insertion_sort.cpp to its original code * Created insertion_sort_recursive.cpp file * Updated documentation and structure in insertion_sort_recursive.cpp * Updated documentation in insertion_sort_recursive.cpp * chore: remove redundant comment * chore:Removed unnecssary whitespace * doc: ease the brief of test * doc: remove redundant lines from main * chore:Removed redundant whitespace --------- Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>
1 parent dd36279 commit faff217

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

sorting/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 Insertion Sort Algorithm
4+
* @author [Dhanush S](https://github.com/Fandroid745)
5+
*
6+
* @details
7+
* Insertion sort is a simple sorting algorithm that builds the final
8+
* sorted array one element at a time. It is much less efficient compared
9+
* to other sorting algorithms like heap sort, merge sort, or quick sort.
10+
*
11+
* However, it has several advantages:
12+
* - Easy to implement.
13+
* - Efficient for small data sets.
14+
* - More efficient than other O(n²) algorithms like selection sort or bubble sort.
15+
* - Stable: it does not change the relative order of elements with equal keys.
16+
*
17+
* Insertion sort works similarly to how people sort playing cards in their hands.
18+
* The algorithm iterates through the list and inserts each element into its correct
19+
* position in the sorted portion of the array.
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+
*
24+
* Example execution:
25+
* 1. Start with the array [4, 3, 2, 5, 1].
26+
* 2. Insert 3 in its correct position: [3, 4, 2, 5, 1].
27+
* 3. Insert 2: [2, 3, 4, 5, 1].
28+
* 4. Continue this until the array is sorted: [1, 2, 3, 4, 5].
29+
*/
30+
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
36+
37+
/**
38+
* @namespace sorting
39+
* @brief Contains sorting algorithms
40+
*/
41+
namespace sorting {
42+
43+
/**
44+
* @brief Insertion Sort Function
45+
*
46+
* @tparam T Type of the array elements
47+
* @param[in,out] arr Array to be sorted
48+
* @param n Size of the array
49+
*/
50+
template <typename T>
51+
void insertionSort(T *arr, int n) {
52+
for (int i = 1; i < n; i++) {
53+
T temp = arr[i];
54+
int j = i - 1;
55+
while (j >= 0 && temp < arr[j]) {
56+
arr[j + 1] = arr[j];
57+
j--;
58+
}
59+
arr[j + 1] = temp;
60+
}
61+
}
62+
63+
/**
64+
* @brief Insertion Sort for a vector
65+
*
66+
* @tparam T Type of the vector elements
67+
* @param [in,out] arr Pointer to the vector to be sorted
68+
*/
69+
template <typename T>
70+
void insertionSort(std::vector<T> *arr) {
71+
size_t n = arr->size();
72+
73+
for (size_t i = 1; i < n; i++) {
74+
T temp = arr->at(i);
75+
int32_t j = i - 1;
76+
while (j >= 0 && temp < arr->at(j)) {
77+
arr->at(j + 1) = arr->at(j);
78+
j--;
79+
}
80+
arr->at(j + 1) = temp;
81+
}
82+
}
83+
84+
} // namespace sorting
85+
86+
/**
87+
* @brief Helper function to create a random array
88+
*
89+
* @tparam T Type of the array elements
90+
* @param arr Array to fill (must be pre-allocated)
91+
* @param N Number of elements in the array
92+
*/
93+
template <typename T>
94+
static void create_random_array(T *arr, int N) {
95+
while (N--) {
96+
double r = (std::rand() % 10000 - 5000) / 100.f;
97+
arr[N] = static_cast<T>(r);
98+
}
99+
}
100+
101+
/**
102+
* @brief self test implementation
103+
* @return void
104+
*/
105+
static void tests() {
106+
int arr1[10] = {78, 34, 35, 6, 34, 56, 3, 56, 2, 4};
107+
std::cout << "Test 1... ";
108+
sorting::insertionSort(arr1, 10);
109+
assert(std::is_sorted(arr1, arr1 + 10));
110+
std::cout << "passed" << std::endl;
111+
112+
int arr2[5] = {5, -3, 7, -2, 1};
113+
std::cout << "Test 2... ";
114+
sorting::insertionSort(arr2, 5);
115+
assert(std::is_sorted(arr2, arr2 + 5));
116+
std::cout << "passed" << std::endl;
117+
118+
float arr3[5] = {5.6, -3.1, -3.0, -2.1, 1.8};
119+
std::cout << "Test 3... ";
120+
sorting::insertionSort(arr3, 5);
121+
assert(std::is_sorted(arr3, arr3 + 5));
122+
std::cout << "passed" << std::endl;
123+
124+
std::vector<float> arr4({5.6, -3.1, -3.0, -2.1, 1.8});
125+
std::cout << "Test 4... ";
126+
sorting::insertionSort(&arr4);
127+
assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
128+
std::cout << "passed" << std::endl;
129+
130+
int arr5[50];
131+
std::cout << "Test 5... ";
132+
create_random_array(arr5, 50);
133+
sorting::insertionSort(arr5, 50);
134+
assert(std::is_sorted(arr5, arr5 + 50));
135+
std::cout << "passed" << std::endl;
136+
137+
float arr6[50];
138+
std::cout << "Test 6... ";
139+
create_random_array(arr6, 50);
140+
sorting::insertionSort(arr6, 50);
141+
assert(std::is_sorted(arr6, arr6 + 50));
142+
std::cout << "passed" << std::endl;
143+
}
144+
145+
/**
146+
* @brief Main function
147+
* @return 0 on successful exit.
148+
*/
149+
int main() {
150+
tests(); /// run self test implementations
151+
return 0;
152+
}

0 commit comments

Comments
 (0)