Searching & Sorting AlgorithmsIntermediate LevelStudy Time: 25 mins

Merge Sort Algorithm

Merge Sort is a stable Divide and Conquer algorithm that recursively splits an array into halves until single elements remain, then merges sorted halves back together.

Loading visualizer workspace...

Real-world analogy: Merging Sorted Decks

Divide
Merge
Hover over items for details

Divide cards into two piles. Sort each pile, then merge them back by comparing the top card of each pile and picking the smaller one.

  • Divide= Splits array into two equal halves.
  • Merge= Combines two sorted sub-arrays into one ordered array.

Complexity analysis

Scenario Simulator: Dataset Size (N = 500)
Presets:
N = 10N = 10,000
Complexity Curve MapMerge Sort: O(N log N) Linearithmic Time
Time (t)Size N (500)N = 500O(N²)O(N log N)O(N)O(log N)O(1)Merge Sort Divid
Time Steps (N=500)

~4,483 steps (Linearithmic)

O(N log N) Linearithmic Time

Space Footprint

O(N) Aux Memory

Memory growth rate as N expands.

Performance Rank
Moderate Tier

Relative efficiency rating for large N.

OperationTime ComplexitySpace Complexity
Best / Average / Worst CaseO(N log N)O(N)

Code implementations

function mergeSort(arr) {
  if (arr.length <= 1) return arr;
  const mid = Math.floor(arr.length / 2);
  const left = mergeSort(arr.slice(0, mid));
  const right = mergeSort(arr.slice(mid));
  return merge(left, right);
}

Real-world applications

Case 01

External Sorting

Sorts huge datasets that exceed RAM capacity using disk chunks.

Case 02

Java Collections.sort()

Used for object reference arrays requiring stable sorting.

Interview questions

Topic Knowledge Test & Assessment

10 Questions Assessment

Merge Sort Knowledge & Skill Test

Validate your conceptual understanding, operation mechanics, and core concepts of Merge Sort.