{
  "slug": "insertion-sort",
  "title": "Insertion Sort",
  "category": "Sorting",
  "difficulty": "easy",
  "complexity": {
    "time": "O(n²)",
    "space": "O(1)"
  },
  "idea": "Keep the left part sorted. Take the next value and slide it left past everything larger, until it sits in the right spot.",
  "code": [
    "for i in 1 .. n-1:",
    "    j = i",
    "    while j > 0 and a[j-1] > a[j]:",
    "        swap a[j-1], a[j]",
    "        j -= 1"
  ],
  "example": {
    "array": [
      6,
      3,
      8,
      1,
      5,
      2
    ]
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 35,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "The first element is a sorted prefix of one. Extend it, one value at a time.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        6,
        3,
        8,
        1,
        5,
        2
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 0,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 1,
      "op": "read",
      "caption": "Take 3 and slide it back into the sorted prefix.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        6,
        3,
        8,
        1,
        5,
        2
      ],
      "focus": [
        null,
        "read",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 1,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 0,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 2,
      "op": "compare",
      "caption": "6 > 3 — they're out of order.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        6,
        3,
        8,
        1,
        5,
        2
      ],
      "focus": [
        "compare",
        "compare",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 1,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 0,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 3,
      "op": "swap",
      "caption": "Swap 6 and 3.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        3,
        6,
        8,
        1,
        5,
        2
      ],
      "focus": [
        "swap",
        "swap",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 1,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 0,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 4,
      "op": "settle",
      "caption": "The prefix through index 1 is sorted.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        3,
        6,
        8,
        1,
        5,
        2
      ],
      "focus": [
        "settled",
        "settled",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 1,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 1,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 5,
      "op": "read",
      "caption": "Take 8 and slide it back into the sorted prefix.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        6,
        8,
        1,
        5,
        2
      ],
      "focus": [
        null,
        null,
        "read",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 2,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 1,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 6,
      "op": "compare",
      "caption": "6 ≤ 8 — it fits here.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        3,
        6,
        8,
        1,
        5,
        2
      ],
      "focus": [
        null,
        "compare",
        "compare",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 2,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 1,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 7,
      "op": "settle",
      "caption": "The prefix through index 2 is sorted.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        3,
        6,
        8,
        1,
        5,
        2
      ],
      "focus": [
        "settled",
        "settled",
        "settled",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 2,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 2,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 8,
      "op": "read",
      "caption": "Take 1 and slide it back into the sorted prefix.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        3,
        6,
        8,
        1,
        5,
        2
      ],
      "focus": [
        null,
        null,
        null,
        "read",
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 3,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 2,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 9,
      "op": "compare",
      "caption": "8 > 1 — they're out of order.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        3,
        6,
        8,
        1,
        5,
        2
      ],
      "focus": [
        null,
        null,
        "compare",
        "compare",
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 3,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 2,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 10,
      "op": "swap",
      "caption": "Swap 8 and 1.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        3,
        6,
        1,
        8,
        5,
        2
      ],
      "focus": [
        null,
        null,
        "swap",
        "swap",
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 3,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 2,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 11,
      "op": "compare",
      "caption": "6 > 1 — they're out of order.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        3,
        6,
        1,
        8,
        5,
        2
      ],
      "focus": [
        null,
        "compare",
        "compare",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 3,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 2,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 12,
      "op": "swap",
      "caption": "Swap 6 and 1.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        3,
        1,
        6,
        8,
        5,
        2
      ],
      "focus": [
        null,
        "swap",
        "swap",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 3,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 2,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 13,
      "op": "compare",
      "caption": "3 > 1 — they're out of order.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        3,
        1,
        6,
        8,
        5,
        2
      ],
      "focus": [
        "compare",
        "compare",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 3,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 2,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 14,
      "op": "swap",
      "caption": "Swap 3 and 1.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        1,
        3,
        6,
        8,
        5,
        2
      ],
      "focus": [
        "swap",
        "swap",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 3,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 2,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 15,
      "op": "settle",
      "caption": "The prefix through index 3 is sorted.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        1,
        3,
        6,
        8,
        5,
        2
      ],
      "focus": [
        "settled",
        "settled",
        "settled",
        "settled",
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 3,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 3,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 16,
      "op": "read",
      "caption": "Take 5 and slide it back into the sorted prefix.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        1,
        3,
        6,
        8,
        5,
        2
      ],
      "focus": [
        null,
        null,
        null,
        null,
        "read",
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 4,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 3,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 17,
      "op": "compare",
      "caption": "8 > 5 — they're out of order.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        3,
        6,
        8,
        5,
        2
      ],
      "focus": [
        null,
        null,
        null,
        "compare",
        "compare",
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 4,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 3,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 18,
      "op": "swap",
      "caption": "Swap 8 and 5.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        1,
        3,
        6,
        5,
        8,
        2
      ],
      "focus": [
        null,
        null,
        null,
        "swap",
        "swap",
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 4,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 3,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 19,
      "op": "compare",
      "caption": "6 > 5 — they're out of order.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        3,
        6,
        5,
        8,
        2
      ],
      "focus": [
        null,
        null,
        "compare",
        "compare",
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 4,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 3,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 20,
      "op": "swap",
      "caption": "Swap 6 and 5.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        1,
        3,
        5,
        6,
        8,
        2
      ],
      "focus": [
        null,
        null,
        "swap",
        "swap",
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 4,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 3,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 21,
      "op": "compare",
      "caption": "3 ≤ 5 — it fits here.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        3,
        5,
        6,
        8,
        2
      ],
      "focus": [
        null,
        "compare",
        "compare",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 4,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 3,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 22,
      "op": "settle",
      "caption": "The prefix through index 4 is sorted.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        3,
        5,
        6,
        8,
        2
      ],
      "focus": [
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 4,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 4,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 23,
      "op": "read",
      "caption": "Take 2 and slide it back into the sorted prefix.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "array": [
        1,
        3,
        5,
        6,
        8,
        2
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        "read"
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 4,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 24,
      "op": "compare",
      "caption": "8 > 2 — they're out of order.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        3,
        5,
        6,
        8,
        2
      ],
      "focus": [
        null,
        null,
        null,
        null,
        "compare",
        "compare"
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 4,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 25,
      "op": "swap",
      "caption": "Swap 8 and 2.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        1,
        3,
        5,
        6,
        2,
        8
      ],
      "focus": [
        null,
        null,
        null,
        null,
        "swap",
        "swap"
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 4,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 26,
      "op": "compare",
      "caption": "6 > 2 — they're out of order.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        3,
        5,
        6,
        2,
        8
      ],
      "focus": [
        null,
        null,
        null,
        "compare",
        "compare",
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 4,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 27,
      "op": "swap",
      "caption": "Swap 6 and 2.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        1,
        3,
        5,
        2,
        6,
        8
      ],
      "focus": [
        null,
        null,
        null,
        "swap",
        "swap",
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 4,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 28,
      "op": "compare",
      "caption": "5 > 2 — they're out of order.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        3,
        5,
        2,
        6,
        8
      ],
      "focus": [
        null,
        null,
        "compare",
        "compare",
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 4,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 29,
      "op": "swap",
      "caption": "Swap 5 and 2.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        1,
        3,
        2,
        5,
        6,
        8
      ],
      "focus": [
        null,
        null,
        "swap",
        "swap",
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 4,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 30,
      "op": "compare",
      "caption": "3 > 2 — they're out of order.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        3,
        2,
        5,
        6,
        8
      ],
      "focus": [
        null,
        "compare",
        "compare",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 4,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 31,
      "op": "swap",
      "caption": "Swap 3 and 2.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        1,
        2,
        3,
        5,
        6,
        8
      ],
      "focus": [
        null,
        "swap",
        "swap",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 4,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 32,
      "op": "compare",
      "caption": "1 ≤ 2 — it fits here.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        2,
        3,
        5,
        6,
        8
      ],
      "focus": [
        "compare",
        "compare",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 4,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 33,
      "op": "settle",
      "caption": "The prefix through index 5 is sorted.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        2,
        3,
        5,
        6,
        8
      ],
      "focus": [
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled"
      ],
      "markers": [
        {
          "name": "i",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 34,
      "op": "settle",
      "caption": "Sorted.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        1,
        2,
        3,
        5,
        6,
        8
      ],
      "focus": [
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled"
      ],
      "markers": [],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 5,
          "tone": "settled"
        }
      ]
    }
  ]
}