The Quickselect algorithm is a selection algorithm that efficiently finds the k-th smallest or largest element in an unsorted array. It is based on the partitioning idea from QuickSort but only recursively processes one side of the partition, leading to an average time complexity of O(n)[1][2][3]. The worst-case time complexity is O(n^2), similar to QuickSort, but this is rare with a good pivot selection strategy, such as choosing a random pivot[1][4][5].
To find the Kth largest element in an array of size N using Quickselect, you can find the (N - K)th smallest element[7][8][9].
Here's a C++ implementation of the Quickselect algorithm to find the Kth largest element:
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
// Partition function similar to QuickSort
// It rearranges the elements such that elements less than the pivot
// are on the left, and elements greater than the pivot are on the right.
// Returns the index of the pivot element after partitioning.
int partition(std::vector<int>& arr, int left, int right) {
// Choose a random pivot and swap it with the last element
// This helps in achieving average O(n) time complexity
srand(time(0));
int random_pivot_index = left + rand() % (right - left + 1);
std::swap(arr[random_pivot_index], arr[right]);
int pivot = arr[right];
int i = (left - 1);
for (int j = left; j <= right - 1; j++) {
if (arr[j] <= pivot) {
i++;
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i + 1], arr[right]);
return (i + 1);
}
// Quickselect function to find the Kth smallest element
int quickSelect(std::vector<int>& arr, int left, int right, int k) {
// If k is within the bounds of the array
if (k > 0 && k <= right - left + 1) {
// Partition the array around a pivot
int index = partition(arr, left, right);
// If pivot is at the kth position
if (index - left == k - 1) {
return arr[index];
}
// If k is less than the pivot index, search in the left subarray
if (index - left > k - 1) {
return quickSelect(arr, left, index - 1, k);
}
// If k is greater than the pivot index, search in the right subarray
// Adjust k for the elements in the left subarray and the pivot
return quickSelect(arr, index + 1, right, k - (index - left + 1));
}
// If k is out of bounds
return -1; // Or throw an exception
}
// Function to find the Kth largest element using Quickselect
int findKthLargest(std::vector<int>& arr, int k) {
int n = arr.size();
// The Kth largest element is the (N - K)th smallest element
return quickSelect(arr, 0, n - 1, n - k + 1);
}
int main() {
std::vector<int> arr = {3, 2, 1, 5, 6, 4};
int k = 2;
int kth_largest = findKthLargest(arr, k);
if (kth_largest != -1) {
std::cout << "The " << k << "th largest element is: " << kth_largest << std::endl;
} else {
std::cout << "Invalid value of k" << std::endl;
}
return 0;
}
Explanation:
partition(std::vector<int>& arr, int left, int right): This function takes a subarray defined by left and right indices, chooses a pivot (randomly in this implementation for better average performance), and rearranges the elements such that all elements less than or equal to the pivot are to its left, and all elements greater than the pivot are to its right. It returns the final index of the pivot.
quickSelect(std::vector<int>& arr, int left, int right, int k): This is the core Quickselect function. It recursively searches for the k-th smallest element within the specified subarray.
- It first partitions the array using the
partition function.
- It then checks the position of the pivot. If the pivot's index is
k-1 (since we're looking for the k-th smallest element, and indices are 0-based), the pivot is the k-th smallest element, and we return it.
- If the pivot's index is greater than
k-1, the k-th smallest element must be in the left subarray, so we recursively call quickSelect on the left side.
- If the pivot's index is less than
k-1, the k-th smallest element must be in the right subarray. We recursively call quickSelect on the right side, adjusting the value of k to reflect the elements already accounted for in the left partition and the pivot.
findKthLargest(std::vector<int>& arr, int k): This helper function takes the array and the desired k for the k-th largest element. Since the Quickselect implementation finds the k-th smallest, we convert the problem of finding the Kth largest to finding the (N - K + 1)th smallest element, where N is the size of the array[7][8][9].
This implementation provides an average time complexity of O(n) because, on average, each partition step significantly reduces the search space[1][4][2]. In the worst case, if the pivot selection consistently results in highly skewed partitions, the complexity can degrade to O(n^2)[1][5][6]. The space complexity is O(log n) on average due to the recursion stack, and O(n) in the worst case[10].