{
  "schema_version": "1.0",
  "codex": "thecodex.expert/coding",
  "type": "algorithm",
  "slug": "algorithms/sorting/insertion-sort",
  "name": "Insertion Sort",
  "confidence": "high",
  "last_verified": "2026-06-01",
  "url": "https://thecodex.expert/coding/algorithms/sorting/insertion-sort/",
  "canonical_definition": "Insertion sort is a comparison-based sorting algorithm that builds the sorted array one element at a time by inserting each unsorted element into its correct position among already-sorted elements \u2014 O(n) on nearly-sorted data, Theta(n^2) average, O(1) space.",
  "complexity": {
    "best": "O(n)",
    "average": "Theta(n^2)",
    "worst": "Theta(n^2)",
    "space": "O(1)",
    "stability": "stable"
  },
  "concept_relationship": {
    "requires_first": [
      "algorithms/sorting",
      "algorithms/complexity"
    ],
    "enables": [
      "algorithms/sorting/heapsort"
    ]
  },
  "sources": [
    {
      "num": 1,
      "citation": "CLRS 4th ed. \u00a72.1",
      "tier": 1
    },
    {
      "num": 2,
      "citation": "Knuth TAOCP Vol.3 \u00a75.2.1",
      "tier": 1
    },
    {
      "num": 3,
      "citation": "Shell 1959 CACM 2(7)",
      "tier": 1
    },
    {
      "num": 4,
      "citation": "Peters 2002 Timsort CPython",
      "tier": 1
    }
  ]
}