Add merge-sort module: top-down and bottom-up merge sort, dry runs, stability, comparison counts against the exact worst case, linked lists, counting inversions, quick sort and TimSort comparisons, and the 2015 program re-run (unstable on ties, quadratic allocation) (17 tests, 15 transcripts)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b6vDJ5fWY5uEZaJFNi
This commit is contained in:
Claude
2026-12-03 23:34:17 +05:30
parent 5419e5057d
commit 5ed8dca130
42 changed files with 1710 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
Top-down merge sort on [38, 27, 43, 3, 9, 82, 10]: merges in execution order
============================================================================
1. a[0..1] [38] + [27] -> [27, 38] 1 comparisons
array now [27, 38, 43, 3, 9, 82, 10]
2. a[2..3] [43] + [3] -> [3, 43] 1 comparisons
array now [27, 38, 3, 43, 9, 82, 10]
3. a[0..3] [27, 38] + [3, 43] -> [3, 27, 38, 43] 3 comparisons
array now [3, 27, 38, 43, 9, 82, 10]
4. a[4..5] [9] + [82] -> [9, 82] 1 comparisons
array now [3, 27, 38, 43, 9, 82, 10]
5. a[4..6] [9, 82] + [10] -> [9, 10, 82] 2 comparisons
array now [3, 27, 38, 43, 9, 10, 82]
6. a[0..6] [3, 27, 38, 43] + [9, 10, 82] -> [3, 9, 10, 27, 38, 43, 82] 6 comparisons
array now [3, 9, 10, 27, 38, 43, 82]
total: 6 merges, 14 comparisons
@@ -0,0 +1,10 @@
The last merge, element by element: [3, 27, 38, 43] + [9, 10, 82]
=================================================================
k=0 3 <= 9, take left 3 output [3]
k=1 9 < 27, take right 9 output [3, 9]
k=2 10 < 27, take right 10 output [3, 9, 10]
k=3 27 <= 82, take left 27 output [3, 9, 10, 27]
k=4 38 <= 82, take left 38 output [3, 9, 10, 27, 38]
k=5 43 <= 82, take left 43 output [3, 9, 10, 27, 38, 43]
k=6 left run empty, copy 82 output [3, 9, 10, 27, 38, 43, 82]
@@ -0,0 +1,17 @@
Bottom-up merge sort on [38, 27, 43, 3, 9, 82, 10]: one block per pass
======================================================================
pass with width 1:
[38] + [27] -> [27, 38]
[43] + [3] -> [3, 43]
[9] + [82] -> [9, 82]
array after the pass [27, 38, 3, 43, 9, 82, 10]
pass with width 2:
[27, 38] + [3, 43] -> [3, 27, 38, 43]
[9, 82] + [10] -> [9, 10, 82]
array after the pass [3, 27, 38, 43, 9, 10, 82]
pass with width 4:
[3, 27, 38, 43] + [9, 10, 82] -> [3, 9, 10, 27, 38, 43, 82]
array after the pass [3, 9, 10, 27, 38, 43, 82]
total comparisons: 14
+7
View File
@@ -0,0 +1,7 @@
Sorting students by marks only: equal marks should keep their input order
=========================================================================
input [Asha:82, Bala:75, Chitra:82, Dev:75, Esha:90]
ties take LEFT (cmp <= 0) [Bala:75, Dev:75, Asha:82, Chitra:82, Esha:90]
Arrays.sort(T[], Comparator) [Bala:75, Dev:75, Asha:82, Chitra:82, Esha:90]
ties take RIGHT (cmp < 0) [Dev:75, Bala:75, Chitra:82, Asha:82, Esha:90] <- the 2015 rule: not stable
@@ -0,0 +1,15 @@
java legacy/MergeSort.java, typing 5, 38, 27, 43, 3, 9 (the sample of the 2015 article)
=======================================================================================
Enter the number of elements:
Enter element 1:
Enter element 2:
Enter element 3:
Enter element 4:
Enter element 5:
Sorted array:
3
9
27
38
43
@@ -0,0 +1,9 @@
new int[arr.length] inside merge(): elements allocated, and bytes measured with ThreadMXBean (MB = 1,000,000 bytes)
====================================================================================================
10,000 seeded random arrays sorted by the 2015 methods vs Arrays.sort: 0 disagreements.
n merges int slots allocated 2015 measured one shared buffer
1,000 999 999,000 4.0 MB 0.01 MB
10,000 9,999 99,990,000 400.1 MB 0.04 MB
40,000 39,999 1,599,960,000 6,400.5 MB 0.16 MB
@@ -0,0 +1,20 @@
Element comparisons. random = new Random(7).nextInt(); worst = built by Data.worstCase
======================================================================================
n input top-down bottom-up skip-sorted worst bound
1,000 random 8,742 8,734 9,437 8,977
1,000 sorted 5,044 5,052 999 8,977
1,000 reversed 4,932 4,932 5,931 8,977
1,000 worst 8,977 8,976 9,976 8,977
1,024 random 8,963 8,963 9,634 9,217
1,024 sorted 5,120 5,120 1,023 9,217
1,024 reversed 5,120 5,120 6,143 9,217
1,024 worst 9,217 9,217 10,240 9,217
100,000 random 1,536,818 1,566,951 1,596,193 1,568,929
100,000 sorted 853,904 877,968 99,999 1,568,929
100,000 reversed 815,024 815,024 915,023 1,568,929
100,000 worst 1,568,929 1,585,582 1,668,928 1,568,929
1,048,576 random 19,646,187 19,646,187 20,337,833 19,922,945
1,048,576 sorted 10,485,760 10,485,760 1,048,575 19,922,945
1,048,576 reversed 10,485,760 10,485,760 11,534,335 19,922,945
1,048,576 worst 19,922,945 19,922,945 20,971,520 19,922,945
+9
View File
@@ -0,0 +1,9 @@
Cross-checks against Arrays.sort and the O(n^2) inversion definition
====================================================================
every array of length 0..8 over 5 values: 488,281 arrays
random arrays, length 0..199 (seed 2026): 20,000 arrays
each checked: top-down, bottom-up, skip-sorted, linked list (top-down), linked list (bottom-up), inversion count
disagreements: 0
stability, records with 5 distinct keys: 5,000 arrays, same object order as Arrays.sort(T[], Comparator): 0 disagreements
+11
View File
@@ -0,0 +1,11 @@
Merge sort on a singly linked list
==================================
before 38 -> 27 -> 43 -> 3 -> 9 -> 82 -> 10 -> null
after 3 -> 9 -> 10 -> 27 -> 38 -> 43 -> 82 -> null
same 7 node objects, relinked: true
1,000,000 random values: top-down and bottom-up both match Arrays.sort
split written with fast = head (instead of head.next), input 2 -> 1 -> null:
java.lang.StackOverflowError
+13
View File
@@ -0,0 +1,13 @@
Counting inversions
===================
[2, 4, 1, 3, 5] 3
[38, 27, 43, 3, 9, 82, 10] 11
[1, 2, 3, 4, 5] 0
[5, 4, 3, 2, 1] 10
reversed array, n = 100,000: 4,999,950,000 (Integer.MAX_VALUE = 2,147,483,647)
the same total kept in an int: 704,982,704
[7, 7, 7, 7] ties take left : 0
[7, 7, 7, 7] ties take right: 6 (wrong: equal values are not an inversion)
+8
View File
@@ -0,0 +1,8 @@
Comparisons: top-down merge sort vs randomized Lomuto quick sort (pivot seed 1)
===============================================================================
n input merge sort quick sort
1,000 random 8,742 10,900
10,000 random 120,548 156,821
100,000 random 1,536,818 2,046,983
20,000 10 distinct keys 253,189 20,103,072
+22
View File
@@ -0,0 +1,22 @@
From lib/src.zip of Java 25.0.5.1
=================================
Arrays.java, line 98:
public static void sort(int[] a) {
DualPivotQuicksort.sort(a, 0, 0, a.length);
}
Arrays.java, line 1034:
public static void sort(Object[] a) {
if (LegacyMergeSort.userRequested)
legacyMergeSort(a);
else
ComparableTimSort.sort(a, 0, a.length, null, 0, 0);
}
Arrays.java, line 989:
Boolean.getBoolean("java.util.Arrays.useLegacyMergeSort");
TimSort.java, line 80:
private static final int MIN_MERGE = 32;
+7
View File
@@ -0,0 +1,7 @@
The midpoint, with indices near the top of the int range (arithmetic only, no array allocated)
==============================================================================================
lo = 1,200,000,000 hi = 1,500,000,000
(lo + hi) / 2 = -797,483,648
lo + (hi - lo) / 2 = 1,350,000,000
(lo + hi) >>> 1 = 1,350,000,000
@@ -0,0 +1,9 @@
Comparisons for n = 100,000: our merge sort vs Arrays.sort(Integer[], countingComparator)
=========================================================================================
input top-down skip-sorted Arrays.sort
random 1,536,818 1,596,193 1,534,762
sorted 853,904 99,999 99,999
reversed 815,024 915,023 99,999
4 sorted runs 953,895 299,990 299,994
sorted, 10 swaps 1,017,088 487,083 101,078
@@ -0,0 +1,17 @@
java download/MergeSort.java, with two inputs typed in
======================================================
typed: 5 38 27 43 3 9
Number of elements: Elements:
Input: [38, 27, 43, 3, 9]
Top-down: [3, 9, 27, 38, 43]
Bottom-up: [3, 9, 27, 38, 43]
Inversions: 7
typed: 7 38 27 43 3 9 82 10
Number of elements: Elements:
Input: [38, 27, 43, 3, 9, 82, 10]
Top-down: [3, 9, 10, 27, 38, 43, 82]
Bottom-up: [3, 9, 10, 27, 38, 43, 82]
Inversions: 11