10
10
* sorts the left half with insertion sort
11
11
* sorts the right half with selection sort
12
12
* created for educational purposes not optimized for speed.
13
- * @author Cesar (https://github.com/cesar-011)
13
+ * @author [ Cesar] (https://github.com/cesar-011)
14
14
* @see quick_sort.cpp, insertion_sort.cpp, selection_sort_iterative.cpp
15
15
*/
16
16
17
- #include < algorithm> // for std::is_sorted
18
- #include < cassert> // for assert
19
- #include < iostream> // for IO
20
- #include < vector> // for vector in test
17
+ #include < algorithm> // / for std::is_sorted
18
+ #include < cassert> // / for assert
19
+ #include < iostream> // / for IO
20
+ #include < vector> // / for vector
21
21
22
22
/* *
23
- * @brief swap two elements of an array
23
+ * @brief swap two elements of a vector
24
+ * @tparam T
25
+ * @param arr
26
+ * @param i
27
+ * @param j
24
28
*/
25
29
template <typename T>
26
- void swap (T * arr, int i, int j) {
30
+ void swap (std::vector<T>& arr, int i, int j) {
27
31
T aux = arr[i];
28
32
arr[i] = arr[j];
29
33
arr[j] = aux;
30
34
}
31
35
32
36
/* *
33
37
* @brief print the array
38
+ * @tparam T
39
+ * @param arr
40
+ * @param size
34
41
*/
35
42
template <typename T>
36
- void print_array (T * arr, int size ) {
37
- for (int i = 0 ; i < size; i++ ) {
38
- std::cout << arr[i] << " " ;
43
+ void print_array (const std::vector<T>& arr) {
44
+ for (const auto & val : arr ) {
45
+ std::cout << val << " " ;
39
46
}
40
47
}
41
48
@@ -52,22 +59,22 @@ namespace sorting {
52
59
namespace hybrid_quick_insert_select {
53
60
54
61
/* *
55
- * @brief Sorts an array using a hybrid of QuickSort, Insertion Sort, and
62
+ * @brief Sorts a vector using a hybrid of QuickSort, Insertion Sort, and
56
63
* Selection Sort.
57
64
*
58
- * This algorithm partitions the array using QuickSort's partitioning scheme.
65
+ * This algorithm partitions the vector using QuickSort's partitioning scheme.
59
66
* It then applies Insertion Sort to the left half and Selection Sort to the
60
67
* right half. This hybrid is intended for educational purposes and not
61
68
* optimized for performance.
62
69
*
63
- * @tparam T Type of the elements in the array . Must support comparison
70
+ * @tparam T Type of the elements in the vector . Must support comparison
64
71
* operators.
65
- * @param arr Pointer to the array to be sorted.
72
+ * @param arr Reference to the vector to be sorted.
66
73
* @param low Starting index of the subarray to sort.
67
- * @param high Ending index of the subarray to sort (inclusive ).
74
+ * @param high Ending index of the subarray to sort (exclusive ).
68
75
*/
69
76
template <typename T>
70
- void hybrid_quick_insertion_selection (T * arr, int low, int high) {
77
+ void hybrid_quick_insertion_selection (std::vector<T>& arr, int low, int high) {
71
78
if (low >= high)
72
79
return ; // Empty range
73
80
// A single iteration of Quicksort partitioning to divide the array into two
@@ -88,7 +95,7 @@ void hybrid_quick_insertion_selection(T *arr, int low, int high) {
88
95
// Insertion at the left
89
96
if (low < f) {
90
97
for (int k = low + 1 ; k <= f; k++) {
91
- int key = arr[k];
98
+ T key = arr[k];
92
99
int j = k - 1 ;
93
100
while (j >= low && arr[j] > key) {
94
101
arr[j + 1 ] = arr[j];
@@ -101,7 +108,7 @@ void hybrid_quick_insertion_selection(T *arr, int low, int high) {
101
108
// Selection at the right
102
109
if (i < high) {
103
110
for (int k = i; k < high; k++) {
104
- int minimum = arr[k];
111
+ T minimum = arr[k];
105
112
int min_ind = k;
106
113
int j = k + 1 ;
107
114
while (j < high) {
@@ -124,47 +131,47 @@ static void test() {
124
131
// Test 1: empty
125
132
{
126
133
std::vector<int > arr = {};
127
- hybrid_quick_insertion_selection (arr. data () , 0 ,
134
+ hybrid_quick_insertion_selection (arr, 0 ,
128
135
static_cast <int >(arr.size ()));
129
136
assert (std::is_sorted (arr.begin (), arr.end ()));
130
137
}
131
138
132
139
// Test 2: one element
133
140
{
134
141
std::vector<int > arr = {42 };
135
- hybrid_quick_insertion_selection (arr. data () , 0 ,
142
+ hybrid_quick_insertion_selection (arr, 0 ,
136
143
static_cast <int >(arr.size ()));
137
144
assert (std::is_sorted (arr.begin (), arr.end ()));
138
145
}
139
146
140
147
// Test 3: positive numbers
141
148
{
142
149
std::vector<int > arr = {1 , 2 , 3 , 4 , 5 };
143
- hybrid_quick_insertion_selection (arr. data () , 0 ,
150
+ hybrid_quick_insertion_selection (arr, 0 ,
144
151
static_cast <int >(arr.size ()));
145
152
assert (std::is_sorted (arr.begin (), arr.end ()));
146
153
}
147
154
148
155
// Test 4: positive and negative numbers
149
156
{
150
157
std::vector<int > arr = {-5 , 4 , -3 , 2 , 1 };
151
- hybrid_quick_insertion_selection (arr. data () , 0 ,
158
+ hybrid_quick_insertion_selection (arr, 0 ,
152
159
static_cast <int >(arr.size ()));
153
160
assert (std::is_sorted (arr.begin (), arr.end ()));
154
161
}
155
162
156
163
// Test 5: repeated elements
157
164
{
158
165
std::vector<int > arr = {3 , 1 , 2 , 3 , 2 , 1 , 4 };
159
- hybrid_quick_insertion_selection (arr. data () , 0 ,
166
+ hybrid_quick_insertion_selection (arr, 0 ,
160
167
static_cast <int >(arr.size ()));
161
168
assert (std::is_sorted (arr.begin (), arr.end ()));
162
169
}
163
170
164
171
// Test 6: negative numbers
165
172
{
166
173
std::vector<int > arr = {-10 , -7 , -8 , -9 , -1 , -5 };
167
- hybrid_quick_insertion_selection (arr. data () , 0 ,
174
+ hybrid_quick_insertion_selection (arr, 0 ,
168
175
static_cast <int >(arr.size ()));
169
176
assert (std::is_sorted (arr.begin (), arr.end ()));
170
177
}
@@ -175,7 +182,7 @@ static void test() {
175
182
for (int i = 0 ; i < 1000 ; ++i) {
176
183
arr[i] = 1000 - i;
177
184
}
178
- hybrid_quick_insertion_selection (arr. data () , 0 ,
185
+ hybrid_quick_insertion_selection (arr, 0 ,
179
186
static_cast <int >(arr.size ()));
180
187
assert (std::is_sorted (arr.begin (), arr.end ()));
181
188
}
@@ -197,13 +204,14 @@ int main() {
197
204
test (); // run self-test implementations
198
205
199
206
// An example
200
- int N = 8 ;
201
- int array[N] = {8 , 5 , 9 , 20 , 2 , 13 , 3 , 1 };
202
- print_array (array, N);
207
+ std::vector<int > array = {8 , 5 , 9 , 20 , 2 , 13 , 3 , 1 };
208
+ print_array (array);
203
209
std::cout << ' \n ' ;
204
210
sorting::hybrid_quick_insert_select::hybrid_quick_insertion_selection (array,
205
- 0 , N);
206
- print_array (array, N);
211
+ 0 ,
212
+ static_cast <int >(array.size ()));
213
+ print_array (array);
207
214
std::cout << ' \n ' ;
208
215
return 0 ;
209
216
}
217
+
0 commit comments