Documente Academic
Documente Profesional
Documente Cultură
Quicksort
Merge sort
Algorithm
Worst case number of comparisons =
O(?)
Sorting Algorithms
Algorithm
Worst Time
Expected
Time
Extra Memory
Insertion sort
O(1)
Merge sort
O(n)
Quick sort
O(1)
Heap sort
O(1)
Quicksort Algorithm
Input: A[1, , n]
Output: A[1, .., n], where A[1]<=A[2]
<=A[n]
Quicksort:
1. if(n<=1) return;
2. Choose the pivot p = A[n]
3. Put all elements less than p on the left; put all
elements lager than p on the right; put p at the
middle. (Partition)
4. Quicksort(the array on the left of p)
5. Quicksort(the array on the right of p)
Quicksort Algorithm
Quicksort example
2
7
3
1
4
3
7
5
5
6
6
Current pivots
Quicksort
Hi, I am nothing
Nothing
3rd
Previous pivots
Nothing Jr.
Quicksort Algorithm
More detail about partition
Input: A[1, , n] (p=A[n])
Output: A[1,k-1, k, k+1, n], where A[1, , k1]<A[k] and A[k+1, n] > A[k], A[k]=p
Partition:
1. t = the tail of smaller array
2. from i= 1 to n-1{
if(A[i]<p) {
exchange A[t+1] with A[i];
update t to the new tail;
}
Quicksort Algorithm
Partition example
tail
Exchange 2 with
A[tail+1]
Do nothing
tail
2
tail
2
tail
Do nothing
Quicksort Algorithm
Partition example
tail
Exchange 1 with
A[tail+1]
Exchange 3 with
A[tail+1]
Do nothing
tail
tail
3
tail
Do nothing
Quicksort Algorithm
The final version of quick sort:
Quicksort(A, p, r){
if(p<r){
//if(n<=1) return;
Analysis of Quicksort
Time complexity
Worst case
Expected
Analysis of Quicksort
Worst
case
The most unbalanced one -- Is it the worst case?
Strict proof
Expected time complexity
Strict proof
2. Hypothesis: when
induction statement: when
Java implementation of
Quicksort
Professional programmers DO NOT
implement sorting algorithms by themselves
We do it for practicing algorithm
implementation techniques and
understanding those algorithms
Code quicksort
Sort.java // the abstract class
Quicksort_Haydon.java // my quicksort
implementation
SortingTester.java // correctness tester