How to resolve the algorithm Quickselect algorithm step by step in the C programming language
How to resolve the algorithm Quickselect algorithm step by step in the C programming language
Table of Contents
Problem Statement
Use the quickselect algorithm on the vector To show the first, second, third, ... up to the tenth largest member of the vector, in order, here on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Quickselect algorithm step by step in the C programming language
This snippet of code implements the quick select algorithm to obtain the kth smallest element in an array.
The function qselect
takes three parameters: the array, the length of the array and the index of the element to be obtained.
It consists of two main loops:
- The first loop divides the array into two subarrays, one containing elements smaller than the last element of the array, and the other containing the elements that are larger or equal than the last element.
The loop iterates over the array and swaps the elements that are smaller than the last element to the beginning of the array, increasing a counter
st
every time a swap is performed. Once the loop is finished, the last element is swapped with the element at indexst
. - The second loop selects the subarray that contains the kth smallest element based on the value of
st
and the value ofk
. Ifst
is equal tok
, then the element at indexst
is the kth smallest element, and it is returned.
If st
is greater than k
, then the kth smallest element is in the subarray containing the elements smaller than the last element, and the qselect
function is called recursively with this subarray and the index k
.
If st
is smaller than k
, then the kth smallest element is in the subarray containing the elements larger than or equal to the last element, and the qselect
function is called recursively with this subarray and the index k-st
.
The main
function calls the qselect
function for different values of k
and prints the result.
The array x
is copied into the array y
using the memcpy
function, as the qselect
function modifies the array passed as a parameter.
Source code in the c programming language
#include <stdio.h>
#include <string.h>
int qselect(int *v, int len, int k)
{
# define SWAP(a, b) { tmp = v[a]; v[a] = v[b]; v[b] = tmp; }
int i, st, tmp;
for (st = i = 0; i < len - 1; i++) {
if (v[i] > v[len-1]) continue;
SWAP(i, st);
st++;
}
SWAP(len-1, st);
return k == st ?v[st]
:st > k ? qselect(v, st, k)
: qselect(v + st, len - st, k - st);
}
int main(void)
{
# define N (sizeof(x)/sizeof(x[0]))
int x[] = {9, 8, 7, 6, 5, 0, 1, 2, 3, 4};
int y[N];
int i;
for (i = 0; i < 10; i++) {
memcpy(y, x, sizeof(x)); // qselect modifies array
printf("%d: %d\n", i, qselect(y, 10, i));
}
return 0;
}
You may also check:How to resolve the algorithm Barnsley fern step by step in the Wren programming language
You may also check:How to resolve the algorithm System time step by step in the ColdFusion programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Rate counter step by step in the Fortran programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the Batch File programming language