Algorithms · Merge Sort and Quick Sort
Merging Procedure
Overview
Merging takes an array in which two adjacent parts are already sorted — part 1 from index to , and part 2 from index to — and combines them into a single sorted list containing all the elements.
It is the combining step of Merge Sort, and it runs in time linear in the total number of elements.
Key concepts
- Merging: given array where to is sorted and to is sorted, combine both parts into one sorted list containing all the elements.
For example, an array may hold one sorted part from index 1 to 4 and another sorted part from index 5 to 8; the result must again be a sorted list containing all the elements. - Out-of-place procedure: merging copies all elements out of their places into new lists, and then writes them back into the original array.
- Infinity sentinel: at the end of every list a value of infinity is written — practically a very large number, whatever is possible by the data structure used to hold the numbers.
- Why infinity is added at the end: it ensures that if one list gets exhausted, the other list is automatically copied into without any modification to the code.
Without infinity, the code would have to check whether one list is over and then copy the remaining list completely. It is only there to simplify matters. - Extra slot: the extra "+1" space in each list is reserved so that the infinities can be placed at the end.
How it works
- Create a new list whose size is exactly the number of elements from to plus 1 ( elements, plus one extra slot).
- Create a second list whose size is exactly the number of elements from to plus 1 ( elements, plus one extra slot).
- Copy the first sorted list into and the second sorted list into .
- At the end of every list, write infinity.
- Write everything back into the original array (overwriting it), using three pointers:
- i — the element being examined in the left list
- j — the element being examined in the right list
- k — the position in array that has to be filled
- Compare and ; whichever is smaller is written into , and that pointer ( or ) is incremented. is incremented at every step.
- Repeat until all elements are written; when infinity is compared with the remaining element of the other list, the remaining elements get copied and the procedure stops.
Pseudocode:
MERGE(A, P, Q, R)
n1 = Q - P + 1 // number of elements in first list
n2 = R - Q // number of elements in second list
create L of size n1 + 1
create R of size n2 + 1
for loop: copy A[P..Q] into L
for loop: copy A[Q+1..R] into R
L[n1 + 1] = infinity
R[n2 + 1] = infinity
i = 1, j = 1
for K = P to R
if L[i] <= R[j]
A[K] = L[i]
i = i + 1
else
A[K] = R[j]
j = j + 1
// K is incremented at every step
Formulas
- — number of elements in the first list
- — number of elements in the second list
- Size of list (one extra slot for infinity)
- Size of list (one extra slot for infinity)
- If one list has size and the other size , size of the final merged list
- Time complexity of merging two lists of sizes and
- Space complexity of merging extra space
Worked examples
Example 1 Array with indices 1 to 8: . Indices 1–4 are sorted and indices 5–8 are sorted.
- Copy first list into (size ):
- Copy second list into (size ):
- points to , points to , points to .
| Comparison | Smaller | Written into A[k] | Pointer incremented |
|---|---|---|---|
| 1 vs 2 | 1 | A[1] = 1 | i |
| 5 vs 2 | 2 | A[2] = 2 | j |
| 5 vs 4 | 4 | A[3] = 4 | j |
| 5 vs 6 | 5 | A[4] = 5 | i |
| 7 vs 6 | 6 | A[5] = 6 | j |
| 7 vs 9 | 7 | A[6] = 7 | i |
| 8 vs 9 | 8 | A[7] = 8 | i |
| ∞ vs 9 | 9 | A[8] = 9 | stop |
Final array : — sorted.
Example 2 (why infinity is needed) Array : . = beginning of first sorted list, = end of first sorted list, = beginning of second sorted list, = end of second sorted list.
- (size 5)
- (size 5)
- points to , points to , points to .
| Comparison | Smaller | Written into A[k] | Pointer incremented |
|---|---|---|---|
| 10 vs 1 | 1 | A[1] = 1 | j |
| 10 vs 5 | 5 | A[2] = 5 | j |
| 10 vs 6 | 6 | A[3] = 6 | j |
| 10 vs 9 | 9 | A[4] = 9 | j |
| 10 vs ∞ | 10 | A[5] = 10 | i |
| 20 vs ∞ | 20 | A[6] = 20 | i |
| 30 vs ∞ | 30 | A[7] = 30 | i |
| 40 vs ∞ | 40 | A[8] = 40 | i |
One entire list () got copied first; the other list () was still left. Because of infinity at the end of , the remaining elements are automatically copied by comparing with infinity — no special check needed in the code.
Comparison
| Aspect | With extra space | Without extra space |
|---|---|---|
| Time complexity | May increase | |
| Space complexity | extra | — |
| Feasibility | Merge method works | Merge method might not work; some other sorting method may be needed |
Revision summary
- has size and has size ; the extra slot holds infinity.
- Copying the elements into the two lists takes time.
- For elements there are comparisons and copies (each element, after being compared, is written down), so writing back takes .
- Total time complexity of merge ; space complexity .
- For two sorted lists of sizes and : final list size , time comparisons and movements, extra space needed .
- Merge is an out-of-place procedure because elements are copied out of their places into auxiliary lists and then merged back into the array.
- Merge needs extra space for input numbers; with that extra space the merge takes time (in-place merging without extra space is possible but more complex or slower).
- Shortcut: put infinity (a very large value) at the end of both lists so that when one list is exhausted, the other is copied automatically without an "is list over?" check — e.g., in Example 2, after is exhausted, are copied by comparing each with .
Previous-year exam insights
- Worst-case number of comparisons to merge two sorted lists of sizes m and n is m+n-1; best case is min(m,n).
Asked inISRO 2018UGC NET 2013GATE 1995 - Merging k sorted lists of total size n pairwise (two at a time) optimally uses a Huffman-style strategy: repeatedly merge the two smallest lists; total cost equals the weighted external path length.
Asked inGATE 2014GATE 1999 - Merging k sorted lists each of size n/k using a min-heap takes time. Asked inGATE 2005
- In the standard merge loop, after termination exactly one of the two arrays is exhausted; the remaining elements of the other array must be copied, so the total number of elements written is n+m and the loop runs while both i<n and j<m.
Asked inGATE 2006 - Merge sort's total number of merge operations on n elements is n-1 (one per internal node of the merge tree); a merge is 'void' if the last element of the left part is ≤ the first element of the right part, i.e., no interleaving occurs.
Asked inGATE 2026 - Straight two-way merge sort on n elements: pass 1 merges runs of size 1 into runs of size 2, pass 2 into runs of size 4, and so on; after the k-th pass, blocks of size are sorted.
Asked inISRO 2015