Skip to content

Commit 1c6d66d

Browse files
author
cesar-011
committed
Applied requested changes
1 parent 840e050 commit 1c6d66d

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

sorting/hybrid_quick_insertion_selection.cpp

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,39 @@
1010
* sorts the left half with insertion sort
1111
* sorts the right half with selection sort
1212
* created for educational purposes not optimized for speed.
13-
* @author Cesar (https://github.com/cesar-011)
13+
* @author [Cesar](https://github.com/cesar-011)
1414
* @see quick_sort.cpp, insertion_sort.cpp, selection_sort_iterative.cpp
1515
*/
1616

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
2121

2222
/**
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
2428
*/
2529
template <typename T>
26-
void swap(T *arr, int i, int j) {
30+
void swap(std::vector<T>& arr, int i, int j) {
2731
T aux = arr[i];
2832
arr[i] = arr[j];
2933
arr[j] = aux;
3034
}
3135

3236
/**
3337
* @brief print the array
38+
* @tparam T
39+
* @param arr
40+
* @param size
3441
*/
3542
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 << " ";
3946
}
4047
}
4148

@@ -52,22 +59,22 @@ namespace sorting {
5259
namespace hybrid_quick_insert_select {
5360

5461
/**
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
5663
* Selection Sort.
5764
*
58-
* This algorithm partitions the array using QuickSort's partitioning scheme.
65+
* This algorithm partitions the vector using QuickSort's partitioning scheme.
5966
* It then applies Insertion Sort to the left half and Selection Sort to the
6067
* right half. This hybrid is intended for educational purposes and not
6168
* optimized for performance.
6269
*
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
6471
* operators.
65-
* @param arr Pointer to the array to be sorted.
72+
* @param arr Reference to the vector to be sorted.
6673
* @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).
6875
*/
6976
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) {
7178
if (low >= high)
7279
return; // Empty range
7380
// 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) {
8895
// Insertion at the left
8996
if (low < f) {
9097
for (int k = low + 1; k <= f; k++) {
91-
int key = arr[k];
98+
T key = arr[k];
9299
int j = k - 1;
93100
while (j >= low && arr[j] > key) {
94101
arr[j + 1] = arr[j];
@@ -101,7 +108,7 @@ void hybrid_quick_insertion_selection(T *arr, int low, int high) {
101108
// Selection at the right
102109
if (i < high) {
103110
for (int k = i; k < high; k++) {
104-
int minimum = arr[k];
111+
T minimum = arr[k];
105112
int min_ind = k;
106113
int j = k + 1;
107114
while (j < high) {
@@ -124,47 +131,47 @@ static void test() {
124131
// Test 1: empty
125132
{
126133
std::vector<int> arr = {};
127-
hybrid_quick_insertion_selection(arr.data(), 0,
134+
hybrid_quick_insertion_selection(arr, 0,
128135
static_cast<int>(arr.size()));
129136
assert(std::is_sorted(arr.begin(), arr.end()));
130137
}
131138

132139
// Test 2: one element
133140
{
134141
std::vector<int> arr = {42};
135-
hybrid_quick_insertion_selection(arr.data(), 0,
142+
hybrid_quick_insertion_selection(arr, 0,
136143
static_cast<int>(arr.size()));
137144
assert(std::is_sorted(arr.begin(), arr.end()));
138145
}
139146

140147
// Test 3: positive numbers
141148
{
142149
std::vector<int> arr = {1, 2, 3, 4, 5};
143-
hybrid_quick_insertion_selection(arr.data(), 0,
150+
hybrid_quick_insertion_selection(arr, 0,
144151
static_cast<int>(arr.size()));
145152
assert(std::is_sorted(arr.begin(), arr.end()));
146153
}
147154

148155
// Test 4: positive and negative numbers
149156
{
150157
std::vector<int> arr = {-5, 4, -3, 2, 1};
151-
hybrid_quick_insertion_selection(arr.data(), 0,
158+
hybrid_quick_insertion_selection(arr, 0,
152159
static_cast<int>(arr.size()));
153160
assert(std::is_sorted(arr.begin(), arr.end()));
154161
}
155162

156163
// Test 5: repeated elements
157164
{
158165
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,
160167
static_cast<int>(arr.size()));
161168
assert(std::is_sorted(arr.begin(), arr.end()));
162169
}
163170

164171
// Test 6: negative numbers
165172
{
166173
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,
168175
static_cast<int>(arr.size()));
169176
assert(std::is_sorted(arr.begin(), arr.end()));
170177
}
@@ -175,7 +182,7 @@ static void test() {
175182
for (int i = 0; i < 1000; ++i) {
176183
arr[i] = 1000 - i;
177184
}
178-
hybrid_quick_insertion_selection(arr.data(), 0,
185+
hybrid_quick_insertion_selection(arr, 0,
179186
static_cast<int>(arr.size()));
180187
assert(std::is_sorted(arr.begin(), arr.end()));
181188
}
@@ -197,13 +204,14 @@ int main() {
197204
test(); // run self-test implementations
198205

199206
// 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);
203209
std::cout << '\n';
204210
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);
207214
std::cout << '\n';
208215
return 0;
209216
}
217+

0 commit comments

Comments
 (0)