{
  "schema_version": "1.0",
  "codex": "thecodex.expert/coding",
  "type": "algorithm",
  "slug": "algorithms/sorting/heapsort",
  "name": "Heapsort",
  "confidence": "high",
  "last_verified": "2026-06-01",
  "url": "https://thecodex.expert/coding/algorithms/sorting/heapsort/",
  "canonical_definition": "Heapsort is a comparison-based in-place sorting algorithm that first builds a max-heap from the input array, then repeatedly extracts the maximum element to produce a sorted sequence \u2014 achieving Theta(n log n) time in all cases with O(1) auxiliary space.",
  "complexity": {
    "best": "Theta(n log n)",
    "average": "Theta(n log n)",
    "worst": "Theta(n log n)",
    "space": "O(1)",
    "stability": "not stable"
  },
  "concept_relationship": {
    "requires_first": [
      "data-structures/heaps",
      "algorithms/sorting"
    ],
    "enables": [
      "algorithms/sorting/insertion-sort"
    ]
  },
  "sources": [
    {
      "num": 1,
      "citation": "CLRS 4th ed. Ch.6",
      "tier": 1
    },
    {
      "num": 2,
      "citation": "Williams 1964 CACM 7(6)",
      "tier": 1
    },
    {
      "num": 3,
      "citation": "Floyd 1964 CACM 7(12)",
      "tier": 1
    },
    {
      "num": 4,
      "citation": "Fredman & Tarjan 1987 JACM 34(3)",
      "tier": 1
    }
  ]
}