1
1
#include < iostream>
2
2
#include < vector>
3
3
#include < algorithm>
4
+ #include < cassert>
4
5
5
6
using namespace std ;
6
7
7
- // Function to partition the array and return the pivot index
8
+ /* *
9
+ * @brief Partition the array and return the pivot index.
10
+ *
11
+ * This function rearranges the elements in the array such that elements
12
+ * less than or equal to the pivot are on the left, and elements greater
13
+ * than the pivot are on the right.
14
+ *
15
+ * @param arr The array of integers to partition.
16
+ * @param left The starting index of the segment to partition.
17
+ * @param right The ending index of the segment to partition.
18
+ * @returns The index of the pivot after partitioning.
19
+ */
8
20
int partition (vector<int >& arr, int left, int right) {
9
21
int pivot = arr[right];
10
22
int i = left;
@@ -19,7 +31,18 @@ int partition(vector<int>& arr, int left, int right) {
19
31
return i;
20
32
}
21
33
22
- // Function to find the k-th smallest element using QuickSelect
34
+ /* *
35
+ * @brief Find the k-th smallest element using QuickSelect algorithm.
36
+ *
37
+ * This function uses a recursive approach to partition the array and find
38
+ * the k-th smallest element.
39
+ *
40
+ * @param arr The array of integers to search.
41
+ * @param left The starting index of the segment to search.
42
+ * @param right The ending index of the segment to search.
43
+ * @param k The index of the k-th smallest element (0-based).
44
+ * @returns The k-th smallest element in the array.
45
+ */
23
46
int quickSelect (vector<int >& arr, int left, int right, int k) {
24
47
if (left == right) return arr[left]; // Base case: only one element
25
48
@@ -34,7 +57,41 @@ int quickSelect(vector<int>& arr, int left, int right, int k) {
34
57
}
35
58
}
36
59
37
- int main () {
60
+ /* *
61
+ * @brief Self-test implementations
62
+ *
63
+ * This function contains test cases to validate the QuickSelect algorithm
64
+ * using assertions.
65
+ *
66
+ * @returns void
67
+ */
68
+ static void test () {
69
+ vector<int > testArr1 = {3 , 2 , 1 , 5 , 6 , 4 };
70
+ assert (quickSelect (testArr1, 0 , testArr1.size () - 1 , 0 ) == 1 );
71
+ assert (quickSelect (testArr1, 0 , testArr1.size () - 1 , 1 ) == 2 );
72
+ assert (quickSelect (testArr1, 0 , testArr1.size () - 1 , 2 ) == 3 );
73
+ assert (quickSelect (testArr1, 0 , testArr1.size () - 1 , 3 ) == 4 );
74
+ assert (quickSelect (testArr1, 0 , testArr1.size () - 1 , 4 ) == 5 );
75
+ assert (quickSelect (testArr1, 0 , testArr1.size () - 1 , 5 ) == 6 );
76
+
77
+ vector<int > testArr2 = {10 , 4 , 5 , 8 , 6 , 11 , 26 };
78
+ assert (quickSelect (testArr2, 0 , testArr2.size () - 1 , 0 ) == 4 );
79
+ assert (quickSelect (testArr2, 0 , testArr2.size () - 1 , 3 ) == 8 );
80
+ assert (quickSelect (testArr2, 0 , testArr2.size () - 1 , 6 ) == 26 );
81
+
82
+ cout << " All tests have successfully passed!\n " ;
83
+ }
84
+
85
+ /* *
86
+ * @brief Main function
87
+ *
88
+ * @param argc Command-line argument count (ignored)
89
+ * @param argv Command-line array of arguments (ignored)
90
+ * @returns 0 on exit
91
+ */
92
+ int main (int argc, char *argv[]) {
93
+ test (); // Run self-test implementations
94
+
38
95
vector<int > arr = {3 , 2 , 1 , 5 , 6 , 4 };
39
96
int k = 2 ; // We want the 2nd smallest element
40
97
0 commit comments