QuickSelect (A Simple Iterative Implementation)
Last Updated :
20 Jul, 2022
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
The QuickSelect algorithm is based QuickSort. The difference is, instead of recurring for both sides (after finding pivot), it recurs only for the part that contains the k-th smallest element. The logic is simple, if index of partitioned element is more than k, then we recur for left part. If index is same as k, we have found the k-th smallest element and we return. If index is less than k, then we recur for right part. This reduces the expected complexity from O(n log n) to O(n), with a worst case of O(n^2).
function quickSelect(list, left, right, k)
if left = right
return list[left]
Select a pivotIndex between left and right
pivotIndex := partition(list, left, right,
pivotIndex)
if k = pivotIndex
return list[k]
else if k < pivotIndex
right := pivotIndex - 1
else
left := pivotIndex + 1
We have discussed a recursive implementation of QuickSelect. In this post, we are going to discuss simple iterative implementation.
C++
#include <bits/stdc++.h>
using namespace std;
int partition( int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for ( int j = low; j <= high - 1; j++) {
if (arr[j] <= pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return (i + 1);
}
int kthSmallest( int a[], int left, int right, int k)
{
while (left <= right) {
int pivotIndex = partition(a, left, right);
if (pivotIndex == k - 1)
return a[pivotIndex];
else if (pivotIndex > k - 1)
right = pivotIndex - 1;
else
left = pivotIndex + 1;
}
return -1;
}
int main()
{
int arr[] = { 10, 4, 5, 8, 11, 6, 26 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 5;
cout << "K-th smallest element is "
<< kthSmallest(arr, 0, n - 1, k);
return 0;
}
|
Java
class GFG
{
static int partition( int arr[],
int low, int high)
{
int temp;
int pivot = arr[high];
int i = (low - 1 );
for ( int j = low; j <= high - 1 ; j++)
{
if (arr[j] <= pivot)
{
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i + 1 ];
arr[i + 1 ] = arr[high];
arr[high] = temp;
return (i + 1 );
}
static int kthSmallest( int a[], int left,
int right, int k)
{
while (left <= right)
{
int pivotIndex = partition(a, left, right);
if (pivotIndex == k - 1 )
return a[pivotIndex];
else if (pivotIndex > k - 1 )
right = pivotIndex - 1 ;
else
left = pivotIndex + 1 ;
}
return - 1 ;
}
public static void main (String[] args)
{
int arr[] = { 10 , 4 , 5 , 8 , 11 , 6 , 26 };
int n = arr.length;
int k = 5 ;
System.out.println( "K-th smallest element is " +
kthSmallest(arr, 0 , n - 1 , k));
}
}
|
Python3
def partition(arr, low, high) :
pivot = arr[high]
i = (low - 1 )
for j in range (low, high) :
if arr[j] < = pivot :
i + = 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1 ], arr[high] = arr[high], arr[i + 1 ]
return (i + 1 )
def kthSmallest(a, left, right, k) :
while left < = right :
pivotIndex = partition(a, left, right)
if pivotIndex = = k - 1 :
return a[pivotIndex]
elif pivotIndex > k - 1 :
right = pivotIndex - 1
else :
left = pivotIndex + 1
return - 1
arr = [ 10 , 4 , 5 , 8 , 11 , 6 , 26 ]
n = len (arr)
k = 5
print ( "K-th smallest element is" ,
kthSmallest(arr, 0 , n - 1 , k))
|
C#
using System;
class GFG
{
static int partition( int []arr,
int low, int high)
{
int temp;
int pivot = arr[high];
int i = (low - 1);
for ( int j = low; j <= high - 1; j++)
{
if (arr[j] <= pivot)
{
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}
static int kthSmallest( int []a, int left,
int right, int k)
{
while (left <= right)
{
int pivotIndex = partition(a, left, right);
if (pivotIndex == k - 1)
return a[pivotIndex];
else if (pivotIndex > k - 1)
right = pivotIndex - 1;
else
left = pivotIndex + 1;
}
return -1;
}
public static void Main (String[] args)
{
int []arr = { 10, 4, 5, 8, 11, 6, 26 };
int n = arr.Length;
int k = 5;
Console.WriteLine( "K-th smallest element is " +
kthSmallest(arr, 0, n - 1, k));
}
}
|
Javascript
<script>
function partition(arr, low, high)
{
let temp;
let pivot = arr[high];
let i = (low - 1);
for (let j = low; j <= high - 1; j++)
{
if (arr[j] <= pivot)
{
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}
function kthSmallest(a, left, right, k)
{
while (left <= right)
{
let pivotIndex = partition(a, left, right);
if (pivotIndex == k - 1)
return a[pivotIndex];
else if (pivotIndex > k - 1)
right = pivotIndex - 1;
else
left = pivotIndex + 1;
}
return -1;
}
let arr = [ 10, 4, 5, 8, 11, 6, 26 ];
let n = arr.length;
let k = 5;
document.write( "K-th smallest element is " + kthSmallest(arr, 0, n - 1, k));
</script>
|
Output:
K-th smallest element is 10
Time Complexity : O(n2)
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...