
Quick sort
Random shuffle is a key for good performance in the quick sort algorithm because it guarantees that the worst case isn't gonna happen(rarely happens).
Tuned quicksort for primitive types;
Worst Case: Array already sorted takes O(n^2) - imp slide # 18
Time Complexity
Best Case: O(nlogn)
Average Case: O(nlogn)
Worst Case: O(n2) (low probability due to randomization)
Randomization helps ensure that the algorithm performs closer to the average case most of the time, providing reliable performance in practical scenarios.
Find kth-Smallest Element using the QuickSelect Algorithm
Description: Implement a program to find the kth smallest element in an unsorted array efficiently using the QuickSelect algorithm, a quicksort algorithm variation. This algorithm allows for finding the kth smallest element in linear time complexity on average.
#include <iostream>
#include <random>
using namespace std;
int partition(int *arr, int s, int e)
{
int i = s, j = e + 1;
while (true)
{
// find item on left to swap
while (arr[++i] < arr[s])
{
if (i == e)
break;
}
// find item on right to swap
while (arr[s] < arr[--j])
{
if (j == s)
break;
}
// check if pointers cross
if (i >= j)
break;
swap(arr[i], arr[j]);
}
swap(arr[s], arr[j]); // swap with partitioning item
return j;
}
void shuffling(int *arr, int n)
{
for (int i = 0; i < n; i++)
{
std::random_device rd;
std::uniform_int_distribution<int> key(0, i);
swap(arr[i], arr[key(rd)]);
}
}
int quickSelect(int *arr, int n, int k)
{
k -= 1;
shuffling(arr, n); // shuffle needed for performance guarantee
int s = 0, e = n - 1;
while (s < e)
{
int j = partition(arr, s, e);
if (j > k)
e = j - 1;
else if (j < k)
s = j + 1;
else
return arr[k];
}
return arr[k];
}
int main()
{
int arr[] = {6, 3, 1, 6, 0, 4, 8};
int size = 7;
cout << quickSelect(arr, size, 5) << endl;
}

Last updated
Was this helpful?