Add quick-sort module: Lomuto and Hoare partition dry runs, comparison/swap/depth counts on sorted, reverse and all-equal input, random pivot, three-way partition, recursion into the smaller side, and the three 2015 programs re-run (one corrupts duplicates, one throws) (16 tests, 14 transcripts)
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b7Rercfj3dzDQowoFA
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Quick Sort in Java, single file. Lomuto and Hoare partition side by side, plus a randomized pivot.
|
||||
*
|
||||
* javac QuickSort.java && java QuickSort
|
||||
* (or, JDK 11+: java QuickSort.java)
|
||||
*
|
||||
* From ankurm.com: https://ankurm.com/implementing-quick-sort-in-java/
|
||||
*/
|
||||
public class QuickSort {
|
||||
|
||||
// ---- Lomuto: pivot = last element; i marks the end of the "<= pivot" zone -----------------
|
||||
static int lomutoPartition(int[] a, int lo, int hi) {
|
||||
int pivot = a[hi];
|
||||
int i = lo - 1;
|
||||
for (int j = lo; j < hi; j++) {
|
||||
if (a[j] <= pivot) {
|
||||
i++;
|
||||
swap(a, i, j);
|
||||
}
|
||||
}
|
||||
swap(a, i + 1, hi);
|
||||
return i + 1; // the pivot's FINAL index
|
||||
}
|
||||
|
||||
static void lomutoSort(int[] a, int lo, int hi) {
|
||||
if (lo < hi) {
|
||||
int p = lomutoPartition(a, lo, hi);
|
||||
lomutoSort(a, lo, p - 1);
|
||||
lomutoSort(a, p + 1, hi);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Hoare: pivot = middle element; two pointers walk towards each other -------------------
|
||||
static int hoarePartition(int[] a, int lo, int hi) {
|
||||
int pivot = a[lo + (hi - lo) / 2];
|
||||
int i = lo - 1;
|
||||
int j = hi + 1;
|
||||
while (true) {
|
||||
do { i++; } while (a[i] < pivot);
|
||||
do { j--; } while (a[j] > pivot);
|
||||
if (i >= j) {
|
||||
return j; // a split point, NOT the pivot's final index
|
||||
}
|
||||
swap(a, i, j);
|
||||
}
|
||||
}
|
||||
|
||||
static void hoareSort(int[] a, int lo, int hi) {
|
||||
if (lo < hi) {
|
||||
int p = hoarePartition(a, lo, hi);
|
||||
hoareSort(a, lo, p); // p stays on the left side
|
||||
hoareSort(a, p + 1, hi);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Lomuto with a random pivot: no input is "the bad one" any more -------------------------
|
||||
static void randomizedSort(int[] a, int lo, int hi, Random rnd) {
|
||||
if (lo < hi) {
|
||||
swap(a, lo + rnd.nextInt(hi - lo + 1), hi);
|
||||
int p = lomutoPartition(a, lo, hi);
|
||||
randomizedSort(a, lo, p - 1, rnd);
|
||||
randomizedSort(a, p + 1, hi, rnd);
|
||||
}
|
||||
}
|
||||
|
||||
static void swap(int[] a, int i, int j) {
|
||||
int t = a[i];
|
||||
a[i] = a[j];
|
||||
a[j] = t;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
System.out.print("Enter the size of array: ");
|
||||
int n = in.nextInt();
|
||||
int[] data = new int[n];
|
||||
System.out.println("Enter " + n + " elements (one per line):");
|
||||
for (int i = 0; i < n; i++) {
|
||||
data[i] = in.nextInt();
|
||||
}
|
||||
System.out.println("Before: " + Arrays.toString(data));
|
||||
|
||||
int[] a = data.clone();
|
||||
lomutoSort(a, 0, n - 1);
|
||||
System.out.println("Lomuto: " + Arrays.toString(a));
|
||||
|
||||
a = data.clone();
|
||||
hoareSort(a, 0, n - 1);
|
||||
System.out.println("Hoare: " + Arrays.toString(a));
|
||||
|
||||
a = data.clone();
|
||||
randomizedSort(a, 0, n - 1, new Random(42));
|
||||
System.out.println("Randomized: " + Arrays.toString(a));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user