sort(A,p,r): if p < r q = partition(A,p,r) sort(A,p,q-1) sort(A,q+1,r)
partition(A,p,r): x = A[p] i = p j = r+1 while i < j while A[i] < x i = i+1 if i == r break while A[j] > x j = j-1 if j == p break exchange A[i] with A[j] exchange A[j] with A[p] return j
Optimization
Change to Insertion-Sort while sub-array’s item less than 5~15
Quick partition: Find middle item and set size 3 to make partition.
Dijkstra: [Quick3Way] for repeat inputs. Best O(N). Bad O(NlgN)
sort(A,p,r): if p < r // [p,lt) <= [lt,gt) <= [gt,r] lt = p gt = r i = p+1 x = a[p] while i <= gt if A[i] < p exchange A[lt] with A[i] lt = lt+1 i = i+1 else if A[i] > p exchange A[i] with A[gt] gt = gt-1 else i = i+1 sort(A,p,lt-1) sort(A,gt+1,r)