Quickselect Algorithm
Quickselect is a selection algorithm to find the k-th smallest element in an unordered list. It is related to the quick sort sorting algorithm.
Examples:
Input: arr[] = {7, 10, 4, 3, 20, 15}
k = 3
Output: 7
Input: arr[] = {7, 10, 4, 3, 20, 15}
k = 4
Output: 10
code link
#include <stdio.h>
#define SWAP(x, y) { int temp = x; x = y; y = temp; }
#define N (sizeof(A)/sizeof(A[0]))
// Partition using Lomuto partition scheme
int partition(int a[], int left, int right, int pivotIndex)
{
// Pick pivotIndex as pivot from the array
int pivot = a[pivotIndex];
// Move pivot to end
SWAP(a[pivotIndex], a[right]);
// elements less than pivot will be pushed to the left of pIndex
// elements more than pivot will be pushed to the right of pIndex
// equal elements can go either way
int pIndex = left;
int i;
// each time we finds an element less than or equal to pivot, pIndex
// is incremented and that element would be placed before the pivot.
for (i = left; i < right; i++)
{
if (a[i] <= pivot)
{
SWAP(a[i], a[pIndex]);
pIndex++;
}
}
// Move pivot to its final place
SWAP (a[pIndex], a[right]);
// return pIndex (index of pivot element)
return pIndex;
}
// Returns the k-th smallest element of list within left..right inclusive
// (i.e. left <= k <= right).
// The search space within the array is changing for each round - but the list
// is still the same size. Thus, k does not need to be updated with each round.
int quickselect(int A[], int left, int right, int k)
{
// If the array contains only one element, return that element
if (left == right)
return A[left];
// select a pivotIndex between left and right
int pivotIndex = left + rand() % (right - left + 1);
pivotIndex = partition(A, left, right, pivotIndex);
// The pivot is in its final sorted position
if (k == pivotIndex)
return A[k];
// if k is less than the pivot index
else if (k < pivotIndex)
return quickselect(A, left, pivotIndex - 1, k);
// if k is more than the pivot index
else
return quickselect(A, pivotIndex + 1, right, k);
}
// main function
int main()
{
int A[] = { 7, 4, 6, 3, 9, 1 };
int k = 2;
printf("K'th smallest element is %d", quickselect(A, 0, N - 1, k));
return 0;
}
c++ function:-
#include <iostream>
#include <vector>
#include <algorithm>
// main function
int main()
{
std::vector<int> a = { 7, 4, 6, 3, 9, 1 };
const std::size_t k = 2;
std::nth_element(a.begin(), a.begin() + k, a.end());
std::cout << "K'th smallest element is " << a[k];
return 0;
}
Quickselect is a selection algorithm to find the k-th smallest element in an unordered list. It is related to the quick sort sorting algorithm.
Examples:
Input: arr[] = {7, 10, 4, 3, 20, 15}
k = 3
Output: 7
Input: arr[] = {7, 10, 4, 3, 20, 15}
k = 4
Output: 10
Hello There,
ReplyDeleteGreat piece on Quick select Algorithm I’m a fan of the ‘flowery’ style Looking forward to more long form articles ??
I am trying to make a program in C with multiple bouncing balls. I know it is a very known program and I have already searched the internet and seen different versions of it. However, because I am new in programming, I didn’t manage to understand them and answer my questions The problem is that I have created a code, but my program doesn’t run properly. To be more exact, I think the part where it reads the data is fine, but then there is a problem in the code of the graphics' library. I cannot understand where the problem is, as it is the first time I use this library.
Super likes !!! for this amazing post. I thinks everyone should bookmark this.
Grazie,
Jake