These are part of my notes for the Coursera course: Algorithms, Pt I (Princeton).
This requires knowledge of Partitioning.
##Quicksort
####Concept
- we know
- partitioning places smaller items on the left and larger items on the right of a selected value
- if we have 2 or 3 values, a partitioning operation will sort them in order
- partition recursively, halving the ranges every time
- each recursion sorts ranges more and more
- will eventually lead to ranges of 2 or 3 values
- will lead to fully sorted array
####Implementation
- shuffle the array to make it randomly distributed (see analysis)
- partition the array
- creates more ordered ranges
- partition both ordered ranges
- recursion of partitioning new more ordered ranges until all is sorted
####Analysis
- the whole process is done in place which means that it does not take any extra memory
- no need for auxiliary array like in Mergesort
- the worst case (complexity) is
~N^2
- this is extrememly unlikely if randomly shuffled
- the average case takes time
~N lgN
- close to
1.39*N lgN
if randomly shuffled
- close to
- makes more compares than Mergesort
- takes less time than Mergesort as it does not have to move memory around (Mergesort does and it is costly)
####Minor improvements
- skip small ranges altogether and do a single pass of Insertion sort at the end
- can also do small ranges one at a time with Insertion sort
- try get partitioning
pivot
s to be near the near the array's median value- take a sample of array elements (between 3 and 7)
- take the median of the sample
####Notes
- not stable in its basic form
- Quicksort is finnicky
- very sensitive to implementation
- can quickly run in quadratic time if not implemented properly