{
  "schema_version": "1.0",
  "codex": "thecodex.expert/coding",
  "type": "algorithm",
  "slug": "algorithms/sorting/merge-sort",
  "name": "Merge Sort",
  "section": "Algorithms",
  "canonical_definition": "Merge sort is a comparison-based sorting algorithm that divides the input array into two halves, recursively sorts each half, and then merges the two sorted halves into a single sorted array \u2014 achieving Theta(n log n) time in all cases with O(n) auxiliary space.",
  "complexity": {
    "best": "Theta(n log n)",
    "average": "Theta(n log n)",
    "worst": "Theta(n log n)",
    "space": "O(n)",
    "stability": "stable"
  },
  "confidence": "high",
  "last_verified": "2026-06-01",
  "url": "https://thecodex.expert/coding/algorithms/sorting/merge-sort/",
  "concept_relationship": {
    "requires_first": [
      "algorithms/sorting",
      "concepts/recursion"
    ],
    "enables": [
      "algorithms/searching"
    ],
    "commonly_confused_with": [
      "O(n) space from merge buffer not call stack",
      "merge sort != always fastest (quicksort faster in practice)",
      "recursion depth is O(log n) not O(n)"
    ]
  },
  "sources": [
    {
      "num": 1,
      "citation": "CLRS 4th ed. \u00a72.3",
      "tier": 1
    },
    {
      "num": 2,
      "citation": "Knuth TAOCP Vol.3 \u00a75.2.4",
      "tier": 1
    },
    {
      "num": 3,
      "citation": "Peters 2002 Timsort (CPython)",
      "tier": 1
    },
    {
      "num": 4,
      "citation": "Frigo et al. 1999 FOCS cache-oblivious",
      "tier": 1
    }
  ]
}