Skip to content

Commit 1beda78

Browse files
Update Quickselect for Finding the k-th Smallest Element.cpp
Summary of the Code ** Partition Function: Splits the array and returns the pivot index. ** QuickSelect Function: Uses the partition function recursively to find the k-th smallest element. ** Test Function: Contains test cases using assert() to verify the correctness of the quickSelect function. ** Main Function: Runs tests and uses the quickSelect function to find the k-th smallest element from the input vector.
1 parent 449d4db commit 1beda78

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

search/Quickselect for Finding the k-th Smallest Element.cpp

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
#include <iostream>
22
#include <vector>
33
#include <algorithm>
4+
#include <cassert>
45

56
using namespace std;
67

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+
*/
820
int partition(vector<int>& arr, int left, int right) {
921
int pivot = arr[right];
1022
int i = left;
@@ -19,7 +31,18 @@ int partition(vector<int>& arr, int left, int right) {
1931
return i;
2032
}
2133

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+
*/
2346
int quickSelect(vector<int>& arr, int left, int right, int k) {
2447
if (left == right) return arr[left]; // Base case: only one element
2548

@@ -34,7 +57,41 @@ int quickSelect(vector<int>& arr, int left, int right, int k) {
3457
}
3558
}
3659

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+
3895
vector<int> arr = {3, 2, 1, 5, 6, 4};
3996
int k = 2; // We want the 2nd smallest element
4097

0 commit comments

Comments
 (0)