Spring 2023
private void quicksort(int[] a, int lo, int hi) { if (hi <= lo) return; int i = lo-1, j = hi; int t, v = a[hi]; while (true) { while (a[++i] < v) ; while (v < a[--j]) if (j == lo) break; if (i >= j) break; t = a[i]; a[i] = a[j]; a[j] = t; } t = a[i]; a[i] = a[hi]; a[hi] = t; quicksort(a, lo, i-1); quicksort(a, i+1, hi); }
private void quicksort(int[] a, int lo, int hi) // lo: start, hi: end { if (hi <= lo) return; int i = lo-1, j = hi; int t, v = a[hi]; while (true) { while (a[++i] < v) ; while (v < a[--j]) if (j == lo) break; if (i >= j) break; t = a[i]; a[i] = a[j]; a[j] = t; } t = a[i]; a[i] = a[hi]; a[hi] = t; quicksort(a, lo, i-1); quicksort(a, i+1, hi); }
private void quicksort(int[] a, int lo, int hi { if (hi <= lo) return; // stop when array is of size 1 or less int i = lo-1, j = hi; int t, v = a[hi]; while (true) { while (a[++i] < v) ; while (v < a[--j]) if (j == lo) break; if (i >= j) break; t = a[i]; a[i] = a[j]; a[j] = t; } t = a[i]; a[i] = a[hi]; a[hi] = t; quicksort(a, lo, i-1); quicksort(a, i+1, hi); }
private void quicksort(int[] a, int lo, int hi { if (hi <= lo) return; int i = lo-1, j = hi; // i: index from left, j: index from right int t, v = a[hi]; while (true) { while (a[++i] < v) ; while (v < a[--j]) if (j == lo) break; if (i >= j) break; t = a[i]; a[i] = a[j]; a[j] = t; } t = a[i]; a[i] = a[hi]; a[hi] = t; quicksort(a, lo, i-1); quicksort(a, i+1, hi); }
private void quicksort(int[] a, int lo, int hi { if (hi <= lo) return; int i = lo-1, j = hi; int t, v = a[hi]; // t: temp var, v: pivot (last element) while (true) { while (a[++i] < v) ; while (v < a[--j]) if (j == lo) break; if (i >= j) break; t = a[i]; a[i] = a[j]; a[j] = t; } t = a[i]; a[i] = a[hi]; a[hi] = t; quicksort(a, lo, i-1); quicksort(a, i+1, hi); }
private void quicksort(int[] a, int lo, int hi { if (hi <= lo) return; int i = lo-1, j = hi; int t, v = a[hi]; while (true) // Do as much as we can { // skip all elements less than the pivot while (a[++i] < v) ; while (v < a[--j]) if (j == lo) break; if (i >= j) break; t = a[i]; a[i] = a[j]; a[j] = t; } t = a[i]; a[i] = a[hi]; a[hi] = t; quicksort(a, lo, i-1); quicksort(a, i+1, hi); }
private void quicksort(int[] a, int lo, int hi { if (hi <= lo) return; int i = lo-1, j = hi; int t, v = a[hi]; while (true) { while (a[++i] < v) ; // skip all elements greater than the pivot while (v < a[--j]) if (j == lo) break; if (i >= j) break; t = a[i]; a[i] = a[j]; a[j] = t; } t = a[i]; a[i] = a[hi]; a[hi] = t; quicksort(a, lo, i-1); quicksort(a, i+1, hi); }
private void quicksort(int[] a, int lo, int hi { if (hi <= lo) return; int i = lo-1, j = hi; int t, v = a[hi]; while (true) { while (a[++i] < v) ; while (v < a[--j]) if (j == lo) break; if (i >= j) break; // stop when all processed t = a[i]; a[i] = a[j]; a[j] = t; } t = a[i]; a[i] = a[hi]; a[hi] = t; quicksort(a, lo, i-1); quicksort(a, i+1, hi); }
private void quicksort(int[] a, int lo, int hi { if (hi <= lo) return; int i = lo-1, j = hi; int t, v = a[hi]; while (true) { while (a[++i] < v) ; while (v < a[--j]) if (j == lo) break; if (i >= j) break; t = a[i]; a[i] = a[j]; a[j] = t; // swap the two irregulars } t = a[i]; a[i] = a[hi]; a[hi] = t; quicksort(a, lo, i-1); quicksort(a, i+1, hi); }
private void quicksort(int[] a, int lo, int hi { if (hi <= lo) return; int i = lo-1, j = hi; int t, v = a[hi]; while (true) { while (a[++i] < v) ; while (v < a[--j]) if (j == lo) break; if (i >= j) break; t = a[i]; a[i] = a[j]; a[j] = t; } t = a[i]; a[i] = a[hi]; a[hi] = t; // Put the pivot in place quicksort(a, lo, i-1); quicksort(a, i+1, hi); }
private void quicksort(int[] a, int lo, int hi { if (hi <= lo) return; int i = lo-1, j = hi; int t, v = a[hi]; while (true) { while (a[++i] < v) ; while (v < a[--j]) if (j == lo) break; if (i >= j) break; t = a[i]; a[i] = a[j]; a[j] = t; } t = a[i]; a[i] = a[hi]; a[hi] = t; quicksort(a, lo, i-1); // recurse left quicksort(a, i+1, hi); // recurse right }
while (a[++i] < v) ;
translates intoLOOP INC I,1 # increment i CMP V,A(I) # compare v with A(i) BL LOOP # branch if less
A – the number of partitioning stages
B – the number of exchanges
C – the number of compares
On a typical computer: \[4C + 11B + 35A\]
The exact coefficients depend on the compiler and the computer architecture
The coefficient of C is significantly lower compared to mergesort
Theorem Quicksort uses, on the average,
to sort an array of N randomly ordered distinct elements.
where \(H_N = \sum_{1\leq k\leq N}{1/k}\) is the harmonic numbers
Proof: Full proof in the book, if you are interested.
\[C_N = N+1 + \frac{1}{N}\sum_{1\leq j\leq N}{(C_{j-1}+C_{N-j})}\]
\[C_N = N+1 + \frac{1}{N}\sum_{1\leq j\leq N}{(C_{j-1}+C_{N-j})}\]
\[C_N = N+1 + \frac{1}{N}\sum_{1\leq j\leq N}{(C_{j-1}+C_{N-j})}\]
\[C_N = N+1 + \frac{2}{N}\sum_{1\leq j\leq N}{C_{j-1}}\] - Multiply by N
\[NC_N = N^2+N + 2\sum_{1\leq j\leq N}{C_{j-1}}\] - and note for N-1
\[(N-1)C_{N-1} = (N-1)^2+N-1 + 2\sum_{1\leq j\leq N-1}{C_{j-1}}\]
\[NC_N - (N-1)C_{N-1}= 2N+2C_{N-1}\] - rearrange
\[NC_N = (N+1)C_{N-1} + 2N\]
\[\frac{C_N}{N+1} = \frac{C_{N-1}}{N} + \frac{2}{N+1}\]
\[\frac{C_N}{N+1} = \frac{C_1}{2} + 2\sum_{3\leq k\leq N+1}{1/k}\]
\[C_N = 2N\ln N - 1.846N\]