Skip to content

Instantly share code, notes, and snippets.

@bwenzel2
Created September 30, 2018 00:58
Show Gist options
  • Save bwenzel2/b520d6b9038060bf1eecc56a279ed94a to your computer and use it in GitHub Desktop.
Save bwenzel2/b520d6b9038060bf1eecc56a279ed94a to your computer and use it in GitHub Desktop.
Pseudocode for the Quick Select algorithm
//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
@Mjeddawi
Copy link

Is this the recursive version or the iterative one??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment