1. Gyanastra
  2. Algorithms
  3. Merge Sort and Quick Sort
  4. Introduction to Merge Sort (Divide and Conquer)
Free lesson, no account needed 3 min read 11 practice questions after it
Asked in past papers7 times
  • GATE CSE19991995
  • ISRO201720152011
  • UGC NET20222017

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 nn, 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

  1. Divide: Divide the array into two halves.
  2. Conquer: Sort the first half, then sort the second half (recursively, by the same procedure).
  3. Combine: Merge the two sorted halves into the final sorted answer.

Stated on an array AA with the task of sorting elements from index PP to index RR:

  1. Find the midpoint of the range PP to RR.
  2. Sort the part of the array from the starting element PP up to the midpoint.
  3. Sort the part from just after the midpoint up to the end RR.
  4. 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 PP to RR: find the midpoint, sort PP to midpoint, sort just after midpoint to RR, 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 Θ(n)\Theta(n) auxiliary space;
    its worst, best and average time are all Θ(nlog⁡n)\Theta(n \log n), and the recurrence is T(n) = 2T(n/2) + Θ(n)\Theta(n).
    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 2k2^{k} (the last run may be shorter); the array is fully sorted after ⌈log⁡2n\log_{2} n⌉ 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 O(n2)O(n^{2}) and its average/best case is O(nlog⁡n)O(n \log n); 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) + Θ(n)\Theta(n) solves by the Master theorem (case 2) to Θ(nlog⁡n)\Theta(n \log n);
    the recursion tree has log⁡n\log n levels each costing Θ(n)\Theta(n).
    Asked inGATE 1995ISRO 2011
  • Merge of two sorted lists of sizes m and n takes at most m+n−1 comparisons and Θ(m+n)\Theta(m+n) time; the merge step is the combine phase and does no sorting of its own.
    Asked inGATE 1999ISRO 2015
Pages 1–2 of 2