Quickselect
Memorize
To be memorize and the analysis.
QuickSelect is an algorithm for finding the kth smallest element in an unsorted array. Searching Algorithms It uses the partition function to rearrange the elements and recursively narrows down the search range based on the position of the pivot.
Quickselect and Quick Sort rely on two subroutines:
- choose-pivot(A)
- Simplest idea: Always select rightmost element in array
- partition(A, p) Partition Algorithm
- Rearrange A and return pivot-index so that
- the pivot-value is in
- all items in are
- all items in are
- Rearrange A and return pivot-index so that
The Algorithm of Quick Select
Pseudocode:
- In this algorithm, we start by choosing a pivot element
p
from the arrayA
. Then, we call the partition function to partition the array around the pivot. The partition function rearranges the elements such that all elements smaller than the pivot are placed before it, and all elements greater than the pivot are placed after it. It also returns the indexi
where the pivot ends up after partitioning. - After partitioning, we check if the index
i
is equal tok
. If it is, we have found the kth smallest element and return it. Ifi
is greater thank
, it means the kth smallest element is in the left partition, so we recursively call QuickSelect on the left partition of the array. Ifi
is less thank
, it means the kth smallest element is in the right partition, so we recursively call QuickSelect on the right partition of the array, adjustingk
accordingly. - The algorithm continues recursively narrowing down the search range until the kth smallest element is found.
Analysis of Quick Select:
Best Case: Worst Case:
Average case:
- ?