{
  "id": "sorting/insertion-sort",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/sorting/insertion-sort",
  "topic": {
    "slug": "sorting",
    "title": "Sorting"
  },
  "title": "Insertion Sort",
  "tagline": "How you actually sort a hand of cards, without being taught.",
  "vizKind": "bars",
  "complexity": {
    "time": {
      "best": "O(n)",
      "average": "O(n²)",
      "worst": "O(n²)"
    },
    "space": "O(1)",
    "growth": "quadratic",
    "note": "Worst case (reversed input) every card travels all the way left: n²/2 moves. Best case (already sorted) every card stays put: just n−1 comparisons and zero swaps."
  },
  "code": {
    "javascript": "function insertionSort(a) {\n  for (let i = 1; i < a.length; i++) {\n    let j = i;\n    while (j > 0 && a[j - 1] > a[j]) {\n      [a[j - 1], a[j]] = [a[j], a[j - 1]];\n      j--;\n    }\n  }\n  return a;\n}",
    "python": "def insertion_sort(a):\n    for i in range(1, len(a)):\n        j = i\n        while j > 0 and a[j - 1] > a[j]:\n            a[j - 1], a[j] = a[j], a[j - 1]\n            j -= 1\n\n    return a\n",
    "java": "int[] insertionSort(int[] a) {\n  for (int i = 1; i < a.length; i++) {\n    int j = i;\n    while (j > 0 && a[j - 1] > a[j]) {\n      int t = a[j]; a[j] = a[j - 1]; a[j - 1] = t;\n      j--;\n    }\n  }\n  return a;\n}",
    "cpp": "vector<int> insertionSort(vector<int> a) {\n  for (int i = 1; i < a.size(); i++) {\n    int j = i;\n    while (j > 0 && a[j - 1] > a[j]) {\n      swap(a[j - 1], a[j]);\n      j--;\n    }\n  }\n  return a;\n}"
  },
  "defaultInput": [
    8,
    3,
    5,
    1,
    9,
    2,
    7
  ],
  "defaultTarget": null,
  "inputHint": "Try a nearly-sorted list like 1 2 4 3 5 6 and watch how little it does.",
  "pitfalls": [
    {
      "wrong": "Starting the outer loop at i = 0.",
      "right": "Start at 1. A hand of one card is already sorted — there is nothing to compare it against."
    },
    {
      "wrong": "Forgetting to stop as soon as the card is in place.",
      "right": "The moment the card on the left is not bigger, you're done — everything further left is already smaller. Break out. Scanning on is wasted work and destroys the O(n) best case."
    },
    {
      "wrong": "Thinking it's 'just bubble sort'.",
      "right": "Bubble sort makes a full pass regardless. Insertion sort stops each card the instant it lands. On nearly-sorted data that's the difference between n² and n."
    }
  ],
  "quiz": [
    {
      "q": "What is insertion sort's best case, and when does it happen?",
      "options": [
        "O(n²), on reversed input",
        "O(n), on already-sorted input",
        "O(log n), on sorted input",
        "O(n), on random input"
      ],
      "answer": 1,
      "why": "If every card is already bigger than the hand, none of them move. One comparison each: O(n). No other simple sort does this."
    },
    {
      "q": "What is the worst possible input for insertion sort?",
      "options": [
        "An already-sorted list",
        "A list of identical values",
        "A list sorted backwards",
        "A random list"
      ],
      "answer": 2,
      "why": "Reversed input means every single card must travel all the way to the front, past everything. Maximum work: n²/2 moves."
    },
    {
      "q": "Real sorting libraries switch to insertion sort for small chunks. Why?",
      "options": [
        "It's the fastest sort ever written",
        "Its low overhead and near-sorted speed beat fancy sorts on tiny inputs",
        "It uses less memory than everything else",
        "For historical reasons only"
      ],
      "answer": 1,
      "why": "Fancy algorithms carry setup costs that don't pay off on 10 items. Insertion sort has almost no overhead and loves nearly-ordered data — so it wins at small sizes."
    }
  ],
  "problems": [
    {
      "name": "Insertion Sort List",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/insertion-sort-list/"
    },
    {
      "name": "Sort an Array",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/sort-an-array/"
    },
    {
      "name": "Sort Colors",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/sort-colors/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 40,
    "frames": [
      {
        "data": [
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          }
        ],
        "codeLine": 1,
        "variables": {},
        "explanation": "Think of holding cards. The left side is your tidy hand; everything to the right is still on the table. Pick up one card at a time and slide it into place.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "active"
          },
          {
            "index": 0,
            "role": "sorted"
          }
        ],
        "codeLine": 3,
        "variables": {
          "i": 1,
          "card": 3
        },
        "explanation": "Pick up 3. Now slide it left until it is sitting in the right spot.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 0
          },
          {
            "name": "j",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "compare"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 1,
          "j": 1,
          "left": 8,
          "card": 3
        },
        "explanation": "8 is bigger than 3, so 3 still has further to travel.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          },
          {
            "index": 1,
            "role": "swap"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 1,
          "j": 0,
          "card": 3
        },
        "explanation": "3 steps over 8.",
        "stats": {
          "comparisons": 1,
          "swaps": 1
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "i": 1,
          "sortedSoFar": 2
        },
        "explanation": "The first 2 cards are now in order among themselves.",
        "stats": {
          "comparisons": 1,
          "swaps": 1
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "active"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          }
        ],
        "codeLine": 3,
        "variables": {
          "i": 2,
          "card": 5
        },
        "explanation": "Pick up 5. Now slide it left until it is sitting in the right spot.",
        "stats": {
          "comparisons": 1,
          "swaps": 1
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 1
          },
          {
            "name": "j",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 2,
          "j": 2,
          "left": 8,
          "card": 5
        },
        "explanation": "8 is bigger than 5, so 5 still has further to travel.",
        "stats": {
          "comparisons": 2,
          "swaps": 1
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 2,
          "j": 1,
          "card": 5
        },
        "explanation": "5 steps over 8.",
        "stats": {
          "comparisons": 2,
          "swaps": 2
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 0
          },
          {
            "name": "j",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "sorted"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 2,
          "j": 1,
          "left": 3,
          "card": 5
        },
        "explanation": "3 is not bigger than 5. This is the right spot — stop here.",
        "stats": {
          "comparisons": 3,
          "swaps": 2
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "i": 2,
          "sortedSoFar": 3
        },
        "explanation": "The first 3 cards are now in order among themselves.",
        "stats": {
          "comparisons": 3,
          "swaps": 2
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "active"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 3,
        "variables": {
          "i": 3,
          "card": 1
        },
        "explanation": "Pick up 1. Now slide it left until it is sitting in the right spot.",
        "stats": {
          "comparisons": 3,
          "swaps": 2
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 2
          },
          {
            "name": "j",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 3,
          "j": 3,
          "left": 8,
          "card": 1
        },
        "explanation": "8 is bigger than 1, so 1 still has further to travel.",
        "stats": {
          "comparisons": 4,
          "swaps": 2
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 3,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 3,
          "j": 2,
          "card": 1
        },
        "explanation": "1 steps over 8.",
        "stats": {
          "comparisons": 4,
          "swaps": 3
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 1
          },
          {
            "name": "j",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 3,
          "j": 2,
          "left": 5,
          "card": 1
        },
        "explanation": "5 is bigger than 1, so 1 still has further to travel.",
        "stats": {
          "comparisons": 5,
          "swaps": 3
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 3,
          "j": 1,
          "card": 1
        },
        "explanation": "1 steps over 5.",
        "stats": {
          "comparisons": 5,
          "swaps": 4
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 0
          },
          {
            "name": "j",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 3,
          "j": 1,
          "left": 3,
          "card": 1
        },
        "explanation": "3 is bigger than 1, so 1 still has further to travel.",
        "stats": {
          "comparisons": 6,
          "swaps": 4
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          },
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 3,
          "j": 0,
          "card": 1
        },
        "explanation": "1 steps over 3.",
        "stats": {
          "comparisons": 6,
          "swaps": 5
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "i": 3,
          "sortedSoFar": 4
        },
        "explanation": "The first 4 cards are now in order among themselves.",
        "stats": {
          "comparisons": 6,
          "swaps": 5
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "active"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 3,
        "variables": {
          "i": 4,
          "card": 9
        },
        "explanation": "Pick up 9. Now slide it left until it is sitting in the right spot.",
        "stats": {
          "comparisons": 6,
          "swaps": 5
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 3
          },
          {
            "name": "j",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 4,
          "j": 4,
          "left": 8,
          "card": 9
        },
        "explanation": "8 is not bigger than 9. This is the right spot — stop here.",
        "stats": {
          "comparisons": 7,
          "swaps": 5
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "i": 4,
          "sortedSoFar": 5
        },
        "explanation": "The first 5 cards are now in order among themselves.",
        "stats": {
          "comparisons": 7,
          "swaps": 5
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 5,
            "role": "active"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 3,
        "variables": {
          "i": 5,
          "card": 2
        },
        "explanation": "Pick up 2. Now slide it left until it is sitting in the right spot.",
        "stats": {
          "comparisons": 7,
          "swaps": 5
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 4
          },
          {
            "name": "j",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 5,
          "j": 5,
          "left": 9,
          "card": 2
        },
        "explanation": "9 is bigger than 2, so 2 still has further to travel.",
        "stats": {
          "comparisons": 8,
          "swaps": 5
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "swap"
          },
          {
            "index": 5,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 5,
          "j": 4,
          "card": 2
        },
        "explanation": "2 steps over 9.",
        "stats": {
          "comparisons": 8,
          "swaps": 6
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 3
          },
          {
            "name": "j",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 5,
          "j": 4,
          "left": 8,
          "card": 2
        },
        "explanation": "8 is bigger than 2, so 2 still has further to travel.",
        "stats": {
          "comparisons": 9,
          "swaps": 6
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "swap"
          },
          {
            "index": 4,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 5,
          "j": 3,
          "card": 2
        },
        "explanation": "2 steps over 8.",
        "stats": {
          "comparisons": 9,
          "swaps": 7
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 2
          },
          {
            "name": "j",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 5,
          "j": 3,
          "left": 5,
          "card": 2
        },
        "explanation": "5 is bigger than 2, so 2 still has further to travel.",
        "stats": {
          "comparisons": 10,
          "swaps": 7
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 3,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 5,
          "j": 2,
          "card": 2
        },
        "explanation": "2 steps over 5.",
        "stats": {
          "comparisons": 10,
          "swaps": 8
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 1
          },
          {
            "name": "j",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 5,
          "j": 2,
          "left": 3,
          "card": 2
        },
        "explanation": "3 is bigger than 2, so 2 still has further to travel.",
        "stats": {
          "comparisons": 11,
          "swaps": 8
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 5,
          "j": 1,
          "card": 2
        },
        "explanation": "2 steps over 3.",
        "stats": {
          "comparisons": 11,
          "swaps": 9
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 0
          },
          {
            "name": "j",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 5,
          "j": 1,
          "left": 1,
          "card": 2
        },
        "explanation": "1 is not bigger than 2. This is the right spot — stop here.",
        "stats": {
          "comparisons": 12,
          "swaps": 9
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "i": 5,
          "sortedSoFar": 6
        },
        "explanation": "The first 6 cards are now in order among themselves.",
        "stats": {
          "comparisons": 12,
          "swaps": 9
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 6,
            "role": "active"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          }
        ],
        "codeLine": 3,
        "variables": {
          "i": 6,
          "card": 7
        },
        "explanation": "Pick up 7. Now slide it left until it is sitting in the right spot.",
        "stats": {
          "comparisons": 12,
          "swaps": 9
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 5
          },
          {
            "name": "j",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 5,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 6,
          "j": 6,
          "left": 9,
          "card": 7
        },
        "explanation": "9 is bigger than 7, so 7 still has further to travel.",
        "stats": {
          "comparisons": 13,
          "swaps": 9
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 5,
            "role": "swap"
          },
          {
            "index": 6,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 6,
          "j": 5,
          "card": 7
        },
        "explanation": "7 steps over 9.",
        "stats": {
          "comparisons": 13,
          "swaps": 10
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 4
          },
          {
            "name": "j",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 6,
          "j": 5,
          "left": 8,
          "card": 7
        },
        "explanation": "8 is bigger than 7, so 7 still has further to travel.",
        "stats": {
          "comparisons": 14,
          "swaps": 10
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "swap"
          },
          {
            "index": 5,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 6,
          "j": 4,
          "card": 7
        },
        "explanation": "7 steps over 8.",
        "stats": {
          "comparisons": 14,
          "swaps": 11
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j-1",
            "index": 3
          },
          {
            "name": "j",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          }
        ],
        "codeLine": 4,
        "variables": {
          "i": 6,
          "j": 4,
          "left": 5,
          "card": 7
        },
        "explanation": "5 is not bigger than 7. This is the right spot — stop here.",
        "stats": {
          "comparisons": 15,
          "swaps": 11
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "i": 6,
          "sortedSoFar": 7
        },
        "explanation": "The first 7 cards are now in order among themselves.",
        "stats": {
          "comparisons": 15,
          "swaps": 11
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 9,
        "variables": {
          "comparisons": 15,
          "swaps": 11
        },
        "explanation": "Done in 15 comparisons. On a nearly-sorted list, insertion sort barely has to do anything — that is its superpower.",
        "stats": {
          "comparisons": 15,
          "swaps": 11
        }
      }
    ]
  }
}