Created
September 30, 2018 00:58
-
-
Save bwenzel2/b520d6b9038060bf1eecc56a279ed94a to your computer and use it in GitHub Desktop.
Pseudocode for the Quick Select algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This is an O(n) algorithm (linear) | |
Given an array A, of size n: | |
1. Pick a random index, p, as the pivot | |
2. Iterate through the array, comparing every element e to p, and counting the total number of elements before p | |
a. if e < p, move e to the left of p | |
b. if e > p, move e to the right of p | |
c. if e == p, leave e where it is | |
3. count the number of elements before p | |
4. if k < p, recurse on a[0...p-1] | |
if k > p, recurse on a[p+1...n] | |
uf k == p, return p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this the recursive version or the iterative one??