Algorithms · Merge Sort and Quick Sort
Introduction to Merge Sort (Divide and Conquer)
Overview
Merge sort sorts an array by repeatedly applying the merge procedure — combining two sorted lists into one sorted list. It is a divide and conquer algorithm: it splits the problem into subproblems, solves them, then combines their solutions.
Key concepts
- Merge sort is a sorting algorithm that uses the merge procedure repeatedly to sort an entire array.
- Merge combines two already-sorted lists into one sorted list.
- Divide and conquer means: given a problem of size , divide it into two (or any number of) subproblems, solve the subproblems first, and then combine the solutions to get the answer for the original problem.
- Merge and merge sort are two different procedures: merge combines two already-sorted lists; merge sort is the overall sorting procedure that calls merge.
How it works
- Divide: Divide the array into two halves.
- Conquer: Sort the first half, then sort the second half (recursively, by the same procedure).
- Combine: Merge the two sorted halves into the final sorted answer.
Stated on an array with the task of sorting elements from index to index :
- Find the midpoint of the range to .
- Sort the part of the array from the starting element up to the midpoint.
- Sort the part from just after the midpoint up to the end .
- Merge the two sorted parts to obtain the final sorted result.
Revision summary
- Merge sort is a divide and conquer algorithm, not a single-pass sort.
- The three phases are divide (into two halves), conquer (sort each half recursively), combine (merge).
- On a range to : find the midpoint, sort to midpoint, sort just after midpoint to , then merge.
- Do not confuse merge (combines two sorted lists) with merge sort (the overall sorting procedure that calls merge).
- The algorithm is best understood through a worked example rather than a formal statement of the procedure.
Previous-year exam insights
- Merge sort is a stable, out-of-place sort requiring auxiliary space;
its worst, best and average time are all , and the recurrence is T(n) = 2T(n/2) + .
Asked inGATE 1995ISRO 2011 - In a straight two-way (bottom-up) merge sort, pass 1 merges adjacent runs of size 1 into sorted runs of size 2, pass 2 produces runs of size 4, and pass k produces sorted runs of size (the last run may be shorter); the array is fully sorted after ⌈⌉ passes.
Asked inGATE 1999ISRO 2015 - Quick sort is also a divide-and-conquer algorithm (partition around a pivot, then recurse), but unlike merge sort its worst case is and its average/best case is ; it is in-place and not stable.
Asked inUGC NET 2022 - Paradigm matching to memorise: merge sort, quick sort, binary search, Strassen matrix multiplication and closest-pair are divide and conquer; Huffman coding, Kruskal/Prim/Dijkstra and fractional knapsack are greedy; Floyd–Warshall, matrix-chain multiplication, optimal polygon triangulation and subset sum are dynamic programming; insertion sort and topological sort are decrease-and-conquer.
Asked inUGC NET 2017ISRO 2017 - Merge sort's recurrence T(n) = 2T(n/2) + solves by the Master theorem (case 2) to ;
the recursion tree has levels each costing .
Asked inGATE 1995ISRO 2011 - Merge of two sorted lists of sizes m and n takes at most m+n−1 comparisons and time; the merge step is the combine phase and does no sorting of its own.
Asked inGATE 1999ISRO 2015