Computer Science / Divide-and-Conquer Sorting
FREE TO EXPLORE
LABORATORY 03 · COMPUTER SCIENCE

Divide-and-Conquer Sorting lab report

A worked divide-and-conquer sorting lab report with benchmark data tables, asymptotic graphs, calculations, uncertainty and conclusions.

WORKED EXAMPLE · BENCHMARK OBSERVATIONS

A complete example laboratory report.

Simulated demonstration data.

This report illustrates formal algorithmic benchmarking, operation counting arithmetic, and regression analysis. Replace its observations with your own benchmark telemetry when preparing a practical submission.

Student: __________________Student ID: __________________Course: __________________Date: __________________Partner: __________________Instructor: __________________

1. Title and aim

Empirical Analysis of Divide-and-Conquer Sorting: Merge Sort, Quick Sort, and Heap Sort. Measure the empirical operation counts (key comparisons and recursive depth) of Merge Sort, Quick Sort, and Heap Sort across datasets of size n from 64 to 4096 elements, verify the divide-and-conquer recurrence T(n) = 2T(n/2) + Θ(n) ⟹ Θ(n log₂ n), and extract the fundamental asymptotic scaling multiplier k_merge using ordinary least-squares linear regression.

2. Abstract

Seven benchmark trials were generated across input sizes n = 64 to n = 4096 elements (recursive merge interval space x = n(log₂ n − 1) from 320 to 45056 intervals). An ordinary least-squares regression of key comparisons C against recursive interval space yielded an empirical linear scaling multiplier of 1.0000 comparisons/interval, demonstrating exact agreement with the theoretical worst-case recurrence bound C(n) = n log₂ n − n + 1 (difference = 0.0000%). The empirical telemetry proves that while Merge Sort, Quick Sort, and Heap Sort all achieve optimal Θ(n log n) comparison scaling, Merge Sort guarantees strictly stable O(n log n) comparisons at the cost of O(n) auxiliary heap space, Quick Sort provides minimal cache footprint with O(log n) stack frames but degrades to O(n²) under deterministic worst-case pivot selection, and Heap Sort achieves guaranteed O(n log n) worst-case time with strictly O(1) auxiliary space, making Introsort and Timsort the industry standard hybrid architectures.

3. Apparatus and fixed settings

4. Theory and graph choice

Divide-and-conquer sorting represents one of the crowning triumphs of modern computer science, proving that the intuitive quadratic barrier Ω(n²) of elementary pairwise sorting can be shattered by recursive problem decomposition. By partitioning a dataset of size n into balanced subproblems of size n/2, sorting each recursively, and recombining the sorted sequences in linear time Θ(n), Merge Sort achieves an optimal Θ(n log₂ n) worst-case time complexity. In this laboratory, we empirically evaluate the exact operational bounds of divide-and-conquer algorithms, explore why Quicksort frequently outperforms Merge Sort on real microprocessors despite sharing an O(n log n) average bound, and analyze the profound space-time trade-offs between auxiliary buffer allocation and in-place heapification.

C = k_merge · n(log₂ n − 1) + b; k_merge = m

The mathematical regression fits the linear model y = mx + b, where x = n(log₂ n − 1) represents the cumulative recursive comparison interval space, y = C represents discrete key comparisons, m represents the divide-and-conquer scaling factor k_merge, and b represents the base-case leaf offset (theoretically b = 1). The gradient has physical units comparisons/interval.

5. Method

  1. Allocate the primary key array with n distinct integer keys for the chosen benchmark size n in [64, 128, 256, 512, 1024, 2048, 4096]. Allocate an identical auxiliary merge scratchpad of size n. Initialize the keys with a randomized permutation using the Knuth-Fisher-Yates shuffle algorithm.
  2. Initiate the recursive divide phase. For subarray [low .. high], check base condition low >= high. If false, compute the overflow-safe midpoint mid = low + ⌊(high - low) / 2⌋. Allocate a new activation record on the call stack and recursively sort the left half [low .. mid] and right half [mid + 1 .. high].
  3. When a subarray reaches size 1 (low == high), immediately terminate recursion with 0 comparisons. Unwind the stack frame to trigger the merging phase on the two returned sorted halves.
  4. Initialize left cursor p1 = low and right cursor p2 = mid + 1. While p1 <= mid and p2 <= high, fetch A[p1] and A[p2] into ALU registers, increment the comparison counter C by 1, and copy the lesser (or equal) element into the auxiliary scratchpad. Advance the corresponding pointer.
  5. Copy any remaining elements from the non-exhausted subarray directly into the scratchpad without additional comparisons. Copy the fully merged sequence from the auxiliary scratchpad back into the primary array buffer across indices [low .. high].
  6. Upon return from the root invocation, record the total worst-case comparison count C_merge into the notebook. Repeat across all benchmark sizes n in [64, 128, 256, 512, 1024, 2048, 4096]. Compute x = n(log₂ n − 1) and perform ordinary least-squares regression to extract the scaling multiplier k_merge.

6. Observations and calculation table

All benchmark observations use strictly controlled memory buffers. Values are recorded deterministically from the algorithmic comparator register. Residuals are computed relative to the ordinary least-squares regression line.

Table 1. Seven simulated algorithmic benchmarks; x and y represent computational state metrics.
No.Array size n (elements) (elements)Key comparisons C_merge (comparisons)Recursive interval space n(log₂ n − 1) (intervals)Key comparisons C (operations)Fitted yResidual
164321320.0000321.0000321.00000.0000
2128769768.0000769.0000769.00000.0000
325617931792.00001793.00001793.00000.0000
451240974096.00004097.00004097.00000.0000
5102492179216.00009217.00009217.00000.0000
620482048120480.000020481.000020481.00000.0000
740964505745056.000045057.000045057.00000.0000

Download the sample observations (CSV) ↓

7. Worked calculation for one observation

Array Size n = 64 keys; Recursion Tree Depth h = log₂(64) = 6 levels. Recursive Interval Space: x = n(log₂ n − 1) = 64 × (6 − 1) = 320 intervals. Worst-Case Key Comparisons: C_merge = n·log₂(n) − n + 1 = 64 × 6 − 64 + 1 = 321 comparisons. Verification: At depth j in [0 .. 5], exactly 2^j subarrays of size 64/2^j are merged with at most (size − 1) comparisons per subproblem.

8. Graph, gradient and final calculation

-6389.40-5048.328149.808819.8422689.0022688.0037228.2036556.1651767.4050424.32Recursive interval space n(log₂ n − 1) (intervals)Key comparisons C (operations)n = 64n = 128n = 256n = 512n = 1024n = 2048n = 4096

Circular markers represent empirical benchmark observations. The solid line represents the ordinary least-squares asymptotic linear fit.

x̄ = 11675.42857; ȳ = 11676.42857
Σ(x − x̄)² = 1.6009e+09
Σ(x − x̄)(y − ȳ) = 1.6009e+09
m = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)² = 1.00000000
b = ȳ − mx̄ = 1.00000000
k_merge = m = 1.0000 comparisons/interval; Intercept b = 1.0000 comparisons (theoretical k = 1.0000, b = 1.0000)

9. Uncertainty and comparison

Percentage difference = |estimate − theoretical value| / |theoretical value| × 100 = 0.0000%.

For n = 7 benchmark observations, Σresidual² = 0.00000; s² = Σresidual²/(n − 2). Gradient standard error = √[s²/Σ(x − x̄)²] = 2.4993e-11. Intercept standard error = √[s²(1/n + x̄²/Σ(x − x̄)²)] = 4.7750e-07.

Propagating the fit parameters yields a fit standard error of 2.4993e-11 comparisons/interval, with an ideal coefficient of determination R² = 1.000000.

In divide-and-conquer algorithmic complexity analysis, the comparison count C_merge(n) = n·log₂(n) − n + 1 for an array of size n = 2^k is an exact deterministic mathematical identity. Unlike continuous physical instrumentation, discrete operation counting in virtual machine execution registers exhibits zero random measurement noise. However, in physical microprocessor hardware, wall-clock execution latency deviates from pure mathematical comparison counts due to hardware microarchitectural phenomena: (1) Memory bus bandwidth contention during auxiliary scratchpad allocation and array copying, where Merge Sort copies 2n elements per level; (2) L1/L2/L3 cache line evictions when the working array size exceeds the CPU cache capacity (typically > 32 KB for L1); (3) Branch predictor mispredictions during the dual-pointer merge loop (`if (A[p1] <= A[p2])`), which flush deep execution pipelines; and (4) Call stack frame allocation overhead and register spilling during recursive bisections. Ordinary least-squares linear regression of observed comparisons against recursive interval space x = n(log₂ n − 1) yields an empirical scaling slope k_merge = 1.0000 comparisons/interval with zero residual error (R² = 1.00000), rigorously verifying the divide-and-conquer complexity bound Θ(n log₂ n).

10. Result and discussion

k_merge = 1.0000 comparisons/interval
Theoretical reference value: 1.0000 comparisons/interval

The empirical benchmark observations perfectly align with the Master Theorem Case 2 solution of the divide-and-conquer recurrence T(n) = 2T(n/2) + Θ(n), yielding T(n) = Θ(n log₂ n). The regression slope m = 1.0000 confirms that the merge step requires at most n − 1 comparisons across each of the log₂ n recursive tree levels. Furthermore, comparison of the three algorithms illustrates the deep microarchitectural trade-offs: Merge Sort requires O(n) auxiliary memory bandwidth for the merge scratchpad but guarantees complete stability and sequential memory streaming. Quick Sort eliminates auxiliary array allocations by partitioning in place around a pivot, achieving superior CPU L1/L2 cache locality, but risks quadratic degradation if recursion trees become unbalanced. Heap Sort enforces an explicit binary heap invariant (A[parent] ≥ A[child]), guaranteeing O(n log n) bounds in strictly O(1) auxiliary space, but suffers from poor cache locality due to non-local child index hops (2i + 1, 2i + 2). This empirical synthesis explains why production runtimes combine these paradigms into hybrid engines such as Timsort and Introsort.

11. Precautions and limitations

The theoretical RAM model assumes uniform constant-time O(1) access for every memory lookup and copy operation. On physical microprocessor architectures, Merge Sort's continuous memory allocation and copying between primary and auxiliary buffers incurs significant memory bus bandwidth saturation and cache-line evictions. Conversely, Quicksort's superior cache locality allows it to outpace Merge Sort in wall-clock time despite executing slightly more comparisons on average. Heap Sort eliminates auxiliary space completely but suffers severe cache misses due to non-local heap indexing strides. The theoretical worst-case key comparison count for Merge Sort on an array of size n = 2^k is exactly C(n) = n·log₂(n) − n + 1. Under ordinary least-squares regression against the recursive interval space x = n(log₂ n − 1), the theoretical slope is exactly k_merge = 1.0000 comparisons per interval, with an intercept of b = 1.0000 comparison and R² = 1.00000. In comparison, Quick Sort exhibits an average comparison count of C_avg(n) ≈ 2·n·ln(n) ≈ 1.386·n·log₂(n), running with superior cache locality and minimal constants, but suffers from an O(n²) worst-case on pathological pivot choices. Heap Sort achieves guaranteed O(n log n) execution strictly in-place with O(1) auxiliary space, but suffers higher constant factors due to irregular memory access strides.

12. Conclusion

This laboratory investigation quantitatively verified the asymptotic complexity, recurrence dynamics, and microarchitectural behavior of divide-and-conquer sorting algorithms. Across benchmark array sizes from 64 to 4096 elements, we verified the recursive merge scaling multiplier k_merge = 1.0000 comparisons/interval with an ideal R² = 1.00000. The investigation established the fundamental information-theoretic lower bound Ω(n log n) for comparison sorts derived from decision tree leaf entropy (log₂(n!) ≈ n log₂ n − 1.44n) and demonstrated why hybrid algorithms that switch between divide-and-conquer and insertion sort at cache-friendly thresholds achieve optimal practical performance.