A merge-sort implementation sorts portions of a sensor-reading array without copying subarrays. Each portion uses a half-open range: it includes the start index and excludes the stop index. A range starting at index sss and stopping at index ttt therefore contains t−st-st−s readings.
For each recursive split, calculate the middle index using
where the brackets mean round downward to an integer. The left child starts at sss and stops at mmm; the right child starts at mmm and stops at ttt.
Complete the table with exact integers. Left length and right length mean the numbers of readings in the two child ranges.
Unlock full course notes by signing up
Separating sortedness from stability
Records arrived in the order A,B,C,DA,B,C,DA,B,C,D, with numerical keys 4,9,4,124,9,4,124,9,4,12, respectively. During an ascending merge sort, the final sorted halves are [(4,A),(9,B)][(4,A),(9,B)][(4,A),(9,B)] and [(4,C),(12,D)][(4,C),(12,D)][(4,C),(12,D)].
The merge selects the right record whenever the current keys are equal. Assume all other merge operations are implemented correctly using the standard array method. Select every true statement.
Finding records lost after the merge loop
A program merges two sorted lists of ticket numbers. A tail is the portion of an input that has not yet been consumed. The following Python function contains a bug:
Trace the function with left = [2, 7, 11] and right = [4, 6, 10, 13, 15]. The incomplete output contains ________ ticket numbers. At termination, an unconsumed tail remains in the ________ input and contains ________ ticket numbers.
Analyzing skipped merges
An invoice system sorts the identifiers
using this modified top-down merge sort:
Tracking buffers that coexist during recursion
A small device uses recursive merge sort to arrange 40 measurement records. Its implementation allocates temporary buffers as follows:
An invocation with zero or one record returns immediately without allocating a buffer.
An invocation with n≥2n\geq2n≥2 records first allocates a buffer containing exactly nnn record slots.