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:
Claude
2026-11-25 22:58:21 +05:30
parent 515302d5ba
commit 6e5764978f
40 changed files with 1765 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
Lomuto partition, traced on [15, 34, 26, 67, 95, 24]
====================================================
partition [0..5] [15, 34, 26, 67, 95, 24], pivot = 24 (last)
j=0: 15 <= 24 -> i=0, swap a[0] and a[0] [15, 34, 26, 67, 95, 24]
j=1: 34 > 24 -> skip
j=2: 26 > 24 -> skip
j=3: 67 > 24 -> skip
j=4: 95 > 24 -> skip
place pivot: swap a[1] and a[5] [15, 24, 26, 67, 95, 34] -> pivot final at index 1
partition [2..5] [26, 67, 95, 34], pivot = 34 (last)
j=2: 26 <= 34 -> i=2, swap a[2] and a[2] [15, 24, 26, 67, 95, 34]
j=3: 67 > 34 -> skip
j=4: 95 > 34 -> skip
place pivot: swap a[3] and a[5] [15, 24, 26, 34, 95, 67] -> pivot final at index 3
partition [4..5] [95, 67], pivot = 67 (last)
j=4: 95 > 67 -> skip
place pivot: swap a[4] and a[5] [15, 24, 26, 34, 67, 95] -> pivot final at index 4
final: [15, 24, 26, 34, 67, 95]
partition calls: 3
+19
View File
@@ -0,0 +1,19 @@
Hoare partition, traced on [15, 34, 26, 67, 95, 24]
===================================================
partition [0..5] [15, 34, 26, 67, 95, 24], pivot = 26 (middle, index 2)
i stops at 1 (34), j stops at 5 (24) -> swap [15, 24, 26, 67, 95, 34]
i=2 j=2 pointers met/crossed -> split after index 2
partition [0..2] [15, 24, 26], pivot = 24 (middle, index 1)
i=1 j=1 pointers met/crossed -> split after index 1
partition [0..1] [15, 24], pivot = 15 (middle, index 0)
i=0 j=0 pointers met/crossed -> split after index 0
partition [3..5] [67, 95, 34], pivot = 95 (middle, index 4)
i stops at 4 (95), j stops at 5 (34) -> swap [15, 24, 26, 67, 34, 95]
i=5 j=4 pointers met/crossed -> split after index 4
partition [3..4] [67, 34], pivot = 67 (middle, index 3)
i stops at 3 (67), j stops at 4 (34) -> swap [15, 24, 26, 34, 67, 95]
i=4 j=3 pointers met/crossed -> split after index 3
final: [15, 24, 26, 34, 67, 95]
partition calls: 5
@@ -0,0 +1,11 @@
Both partitions on the sample inputs of the other two 2015 posts
================================================================
input: [15, 32, 45, 25, 34, 29, 14]
Lomuto: [14, 15, 25, 29, 32, 34, 45] (5 partition calls)
Hoare: [14, 15, 25, 29, 32, 34, 45] (6 partition calls)
input: [15, 48, 66, 12, 48, 74, 12, 24, 56]
Lomuto: [12, 12, 15, 24, 48, 48, 56, 66, 74] (5 partition calls)
Hoare: [12, 12, 15, 24, 48, 48, 56, 66, 74] (8 partition calls)
@@ -0,0 +1,10 @@
The 2015 temp-array version (post 2559): fine on distinct keys, corrupts duplicates
===================================================================================
distinct keys [15, 32, 45, 25, 34, 29, 14] -> [14, 15, 25, 29, 32, 34, 45] (correct)
duplicates [3, 1, 3, 2] -> [1, 2, 3, 0] (expected [1, 2, 3, 3])
the second 3 was never copied into temp[]; the leftover slot kept its initial 0.
10,000 seeded random arrays (length 2..8, keys 1..9): 6,479 contained a duplicate; 6,479 came back wrong.
every wrong answer was an array with a duplicate; no array of distinct keys came back wrong.
@@ -0,0 +1,9 @@
The 2015 first-pivot version (post 2530): the loop condition reads x[i] before checking i <= r
==============================================================================================
the 2015 sample [15, 48, 66, 12, 48, 74, 12, 24, 56] -> [12, 12, 15, 24, 48, 48, 56, 66, 74] (correct)
input [3, 1, 2]: ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
same input, operands of the && swapped: [1, 2, 3]
10,000 seeded random arrays (length 1..8, keys 0..19): original threw on 4,680; fixed version wrong on 0.
+31
View File
@@ -0,0 +1,31 @@
Comparisons / swaps / deepest recursion, n = 5000
=================================================
n(n-1)/2 = 12,497,500 (what 'quadratic' means for this n)
input scheme comparisons swaps depth
random LOMUTO_LAST 74,221 38,781 32
random LOMUTO_RANDOM 72,503 32,022 27
random HOARE_MIDDLE 86,170 14,464 27
random THREE_WAY 114,975 46,953 26
already sorted LOMUTO_LAST 12,497,500 0 5,000
already sorted LOMUTO_RANDOM 68,422 4,948 27
already sorted HOARE_MIDDLE 66,807 0 14
already sorted THREE_WAY 86,511 28,254 19
reverse sorted LOMUTO_LAST 12,497,500 2,500 5,000
reverse sorted LOMUTO_RANDOM 71,483 31,556 28
reverse sorted HOARE_MIDDLE 66,808 2,500 14
reverse sorted THREE_WAY 86,142 28,030 19
all equal LOMUTO_LAST 12,497,500 0 5,000
all equal LOMUTO_RANDOM 12,497,500 4,991 5,000
all equal HOARE_MIDDLE 69,606 29,804 14
all equal THREE_WAY 10,000 0 2
10 distinct keys LOMUTO_LAST 1,266,199 7,876 545
10 distinct keys LOMUTO_RANDOM 1,266,490 14,752 542
10 distinct keys HOARE_MIDDLE 77,651 25,334 21
10 distinct keys THREE_WAY 35,969 14,705 7
@@ -0,0 +1,16 @@
Lomuto vs Hoare on random distinct-ish keys (seed 7)
====================================================
n scheme comparisons swaps
1,000 LOMUTO_LAST 10,128 4,547
1,000 HOARE_MIDDLE 15,679 2,301
Hoare used 0.51x the swaps of Lomuto
10,000 LOMUTO_LAST 155,168 66,283
10,000 HOARE_MIDDLE 193,958 30,750
Hoare used 0.46x the swaps of Lomuto
100,000 LOMUTO_LAST 2,050,086 1,016,714
100,000 HOARE_MIDDLE 2,377,190 387,156
Hoare used 0.38x the swaps of Lomuto
+28
View File
@@ -0,0 +1,28 @@
Random pivot on already-sorted input, n = 5000, seeds 1..20
===========================================================
Lomuto, last element as pivot: 12,497,500 comparisons
seed 1: 72,954 comparisons, depth 29
seed 2: 70,214 comparisons, depth 26
seed 3: 67,134 comparisons, depth 29
seed 4: 67,613 comparisons, depth 25
seed 5: 85,451 comparisons, depth 34
seed 6: 73,353 comparisons, depth 29
seed 7: 68,648 comparisons, depth 29
seed 8: 67,756 comparisons, depth 26
seed 9: 71,645 comparisons, depth 29
seed 10: 69,475 comparisons, depth 25
seed 11: 70,826 comparisons, depth 27
seed 12: 70,267 comparisons, depth 27
seed 13: 73,057 comparisons, depth 31
seed 14: 72,339 comparisons, depth 31
seed 15: 71,259 comparisons, depth 27
seed 16: 71,299 comparisons, depth 28
seed 17: 71,051 comparisons, depth 29
seed 18: 73,109 comparisons, depth 28
seed 19: 71,353 comparisons, depth 29
seed 20: 72,255 comparisons, depth 27
over 20 seeds: min 67,134, max 85,451 comparisons, deepest recursion 34
n * log2(n) = 61,439
@@ -0,0 +1,7 @@
20,000 keys drawn from only 10 distinct values
==============================================
scheme comparisons swaps depth
LOMUTO_LAST 20,089,334 26,134 2,082
HOARE_MIDDLE 338,316 121,964 23
THREE_WAY 102,334 44,128 6
+9
View File
@@ -0,0 +1,9 @@
Sorted input, n = 100,000, on a thread with a 256 KB stack
==========================================================
Lomuto.sort (recurse into both sides): StackOverflowError
StackSafe.sort (smaller side first): sorted, deepest recursion = 2
log2(100000) = 16.6
The depth at which a StackOverflowError appears depends on the JVM, the JIT state and -Xss;
what is stable is that plain recursion needs ~n frames here and StackSafe needs at most about log2(n).
+10
View File
@@ -0,0 +1,10 @@
20,000 seeded random arrays (length 0..40, keys -20..20) per version, compared with Arrays.sort
===============================================================================================
Lomuto 20,000 arrays, 0 disagreements
Hoare 20,000 arrays, 0 disagreements
ThreeWay 20,000 arrays, 0 disagreements
Randomized (seed 1) 20,000 arrays, 0 disagreements
StackSafe 20,000 arrays, 0 disagreements
LomutoQuickSort2015 (post 2531) 20,000 arrays, 0 disagreements
FirstPivot, fixed (post 2530) 20,000 arrays, 0 disagreements
+14
View File
@@ -0,0 +1,14 @@
Hoare mistakes that look reasonable
===================================
1. last element as pivot with Hoare's (lo,p) / (p+1,hi) recursion, input [1, 2]:
StackOverflowError (the partition returns p = 1 = hi, so (lo, p) is the same range again)
2. Hoare partition, but recursing on (lo, p-1) and (p+1, hi) as Lomuto does, input [2, 3, 1]:
result [2, 1, 3] (expected [1, 2, 3])
all 6 permutations of 1..3: 1 come back unsorted
all 24 permutations of 1..4: 8 come back unsorted
all 120 permutations of 1..5: 52 come back unsorted
the real Hoare.sort on the same input: [1, 2, 3]
+21
View File
@@ -0,0 +1,21 @@
What Arrays.sort really does (read from this JDK's lib/src.zip)
===============================================================
JDK: 25.0.5.1+2-LTS
java/util/Arrays.java:
99 DualPivotQuicksort.sort(a, 0, 0, a.length);
1034 public static void sort(Object[] a) {
1223 public static <T> void sort(T[] a, Comparator<? super T> c) {
1038 ComparableTimSort.sort(a, 0, a.length, null, 0, 0); (inside sort(Object[]))
lines in Arrays.java that call TimSort.sort(: 8
java/util/DualPivotQuicksort.java, the constants and the fallback:
69 private static final int MAX_INSERTION_SORT_SIZE = 44;
64 private static final int MAX_MIXED_INSERTION_SORT_SIZE = 65;
119 private static final int DELTA = 3 << 1;
124 private static final int MAX_RECURSION_DEPTH = 64 * DELTA;
285 if ((bits += DELTA) > MAX_RECURSION_DEPTH) {
286 heapSort(a, low, high);
287 return;
288 }
@@ -0,0 +1,24 @@
java download/QuickSort.java, with the sample inputs of the three 2015 posts
============================================================================
session: 7 elements (post 2559)
Enter the size of array: Enter 7 elements (one per line):
Before: [15, 32, 45, 25, 34, 29, 14]
Lomuto: [14, 15, 25, 29, 32, 34, 45]
Hoare: [14, 15, 25, 29, 32, 34, 45]
Randomized: [14, 15, 25, 29, 32, 34, 45]
session: 9 elements with duplicates (post 2530)
Enter the size of array: Enter 9 elements (one per line):
Before: [15, 48, 66, 12, 48, 74, 12, 24, 56]
Lomuto: [12, 12, 15, 24, 48, 48, 56, 66, 74]
Hoare: [12, 12, 15, 24, 48, 48, 56, 66, 74]
Randomized: [12, 12, 15, 24, 48, 48, 56, 66, 74]
session: 6 elements (post 2531)
Enter the size of array: Enter 6 elements (one per line):
Before: [15, 34, 26, 67, 95, 24]
Lomuto: [15, 24, 26, 34, 67, 95]
Hoare: [15, 24, 26, 34, 67, 95]
Randomized: [15, 24, 26, 34, 67, 95]