{
  "id": "sorting/shell-sort",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/sorting/shell-sort",
  "topic": {
    "slug": "sorting",
    "title": "Sorting"
  },
  "title": "Shell Sort",
  "tagline": "Insertion sort, but values are allowed to leap instead of crawl.",
  "vizKind": "bars",
  "complexity": {
    "time": {
      "best": "O(n log n)",
      "average": "≈ O(n^1.3)",
      "worst": "O(n²)"
    },
    "space": "O(1)",
    "growth": "quadratic",
    "note": "Genuinely hard to pin down — the complexity depends on the gap sequence, and the optimal sequence is still an open research problem. With halving gaps it lands around n^1.5 in practice: far better than insertion sort, not as good as merge sort."
  },
  "code": {
    "javascript": "function shellSort(a) {\n  const n = a.length;\n  for (let gap = n >> 1; gap > 0; gap >>= 1) {\n    for (let i = gap; i < n; i++) {\n      let j = i;\n      while (j >= gap && a[j - gap] > a[j]) {\n        [a[j - gap], a[j]] = [a[j], a[j - gap]];\n        j -= gap;\n      }\n    }\n  }\n  return a;\n}",
    "python": "def shell_sort(a):\n    n = len(a)\n    gap = n // 2\n    while gap > 0:\n        for i in range(gap, n):\n            j = i\n            while j >= gap and a[j - gap] > a[j]:\n                a[j - gap], a[j] = a[j], a[j - gap]\n                j -= gap\n        gap //= 2\n    return a\n",
    "java": "int[] shellSort(int[] a) {\n  int n = a.length;\n  for (int gap = n / 2; gap > 0; gap /= 2) {\n    for (int i = gap; i < n; i++) {\n      int j = i;\n      while (j >= gap && a[j - gap] > a[j]) {\n        int t = a[j]; a[j] = a[j - gap]; a[j - gap] = t;\n        j -= gap;\n      }\n    }\n  }\n  return a;\n}",
    "cpp": "vector<int> shellSort(vector<int> a) {\n  int n = a.size();\n  for (int gap = n / 2; gap > 0; gap /= 2) {\n    for (int i = gap; i < n; i++) {\n      int j = i;\n      while (j >= gap && a[j - gap] > a[j]) {\n        swap(a[j - gap], a[j]);\n        j -= gap;\n      }\n    }\n  }\n  return a;\n}"
  },
  "defaultInput": [
    9,
    8,
    3,
    7,
    5,
    6,
    4,
    1
  ],
  "defaultTarget": null,
  "inputHint": "Watch the gap shrink: 4, then 2, then 1. Values leap, then settle.",
  "pitfalls": [
    {
      "wrong": "Forgetting the final gap-of-1 pass.",
      "right": "Without it the list is not sorted — only the gapped groups are. The last pass is what actually finishes the job."
    },
    {
      "wrong": "Comparing a[i] with a[i-1] inside a gapped pass.",
      "right": "Within a pass of gap g, you compare a[j] with a[j-g], not a[j-1]. Mixing that up turns it straight back into insertion sort."
    },
    {
      "wrong": "Assuming any gap sequence is as good as any other.",
      "right": "It matters a lot. Halving is simple but not optimal; sequences like Ciura's are measurably faster. This is one of the few algorithms where the constant really is the research."
    }
  ],
  "quiz": [
    {
      "q": "What problem with insertion sort does shell sort exist to fix?",
      "options": [
        "It uses too much memory",
        "A value far from home can only crawl one step at a time",
        "It can't handle duplicates",
        "It needs sorted input"
      ],
      "answer": 1,
      "why": "Insertion sort moves values by adjacent swaps only, so a small value at the far end must crawl the entire length. Shell sort lets it leap a whole gap at once."
    },
    {
      "q": "What is the last pass of shell sort?",
      "options": [
        "A merge sort",
        "An ordinary insertion sort (gap = 1)",
        "A binary search",
        "A heapify"
      ],
      "answer": 1,
      "why": "The final gap is always 1, which is plain insertion sort. It's fast because the earlier gapped passes have already left the list nearly sorted — insertion sort's best case."
    },
    {
      "q": "What is unusual about shell sort's time complexity?",
      "options": [
        "It's always exactly O(n)",
        "It depends on the gap sequence, and the best sequence is still unknown",
        "It has no worst case",
        "It's the same as bubble sort"
      ],
      "answer": 1,
      "why": "Its complexity is tied to the gap sequence you pick, and finding the optimal one remains an open problem — genuinely rare for an algorithm this old and this simple."
    }
  ],
  "problems": [
    {
      "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/"
    },
    {
      "name": "Height Checker",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/height-checker/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 38,
    "frames": [
      {
        "data": [
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c7-1",
            "value": 1
          }
        ],
        "pointers": [],
        "highlights": [],
        "codeLine": 1,
        "variables": {},
        "explanation": "Shell sort is insertion sort that stops crawling. It compares values far apart first, so a small value stuck at the end can leap most of the way home in a single move.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": [
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c7-1",
            "value": 1
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "pivot"
          }
        ],
        "codeLine": 3,
        "variables": {
          "gap": 4
        },
        "explanation": "New pass with gap 4: only compare values 4 apart. Everything 4 apart gets put in order among itself.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c7-1",
            "value": 1
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 0
          },
          {
            "name": "j",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 4,
          "left": 9,
          "right": 5
        },
        "explanation": "9 is bigger than 5, and they're 4 apart. Swap them — 5 jumps 4 places left in one go.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c7-1",
            "value": 1
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          },
          {
            "index": 4,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "gap": 4,
          "moved": 5,
          "swaps": 1
        },
        "explanation": "5 leaps 4 places left. Insertion sort would have needed 4 separate steps for that.",
        "stats": {
          "comparisons": 1,
          "swaps": 1
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c7-1",
            "value": 1
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 1
          },
          {
            "name": "j",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 4,
          "left": 8,
          "right": 6
        },
        "explanation": "8 is bigger than 6, and they're 4 apart. Swap them — 6 jumps 4 places left in one go.",
        "stats": {
          "comparisons": 2,
          "swaps": 1
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c7-1",
            "value": 1
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 5,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "gap": 4,
          "moved": 6,
          "swaps": 2
        },
        "explanation": "6 leaps 4 places left. Insertion sort would have needed 4 separate steps for that.",
        "stats": {
          "comparisons": 2,
          "swaps": 2
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c7-1",
            "value": 1
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 2
          },
          {
            "name": "j",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 4,
          "left": 3,
          "right": 4
        },
        "explanation": "3 is not bigger than 4. This pair is fine.",
        "stats": {
          "comparisons": 3,
          "swaps": 2
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c7-1",
            "value": 1
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 3
          },
          {
            "name": "j",
            "index": 7
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 7,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 4,
          "left": 7,
          "right": 1
        },
        "explanation": "7 is bigger than 1, and they're 4 apart. Swap them — 1 jumps 4 places left in one go.",
        "stats": {
          "comparisons": 4,
          "swaps": 2
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c3-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 3,
            "role": "swap"
          },
          {
            "index": 7,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "gap": 4,
          "moved": 1,
          "swaps": 3
        },
        "explanation": "1 leaps 4 places left. Insertion sort would have needed 4 separate steps for that.",
        "stats": {
          "comparisons": 4,
          "swaps": 3
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c3-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "pivot"
          },
          {
            "index": 2,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "pivot"
          },
          {
            "index": 6,
            "role": "pivot"
          }
        ],
        "codeLine": 3,
        "variables": {
          "gap": 2
        },
        "explanation": "New pass with gap 2: only compare values 2 apart. Everything 2 apart gets put in order among itself.",
        "stats": {
          "comparisons": 4,
          "swaps": 3
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c3-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 0
          },
          {
            "name": "j",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 2,
          "left": 5,
          "right": 3
        },
        "explanation": "5 is bigger than 3, and they're 2 apart. Swap them — 3 jumps 2 places left in one go.",
        "stats": {
          "comparisons": 5,
          "swaps": 3
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c3-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          },
          {
            "index": 2,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "gap": 2,
          "moved": 3,
          "swaps": 4
        },
        "explanation": "3 leaps 2 places left. Insertion sort would have needed 2 separate steps for that.",
        "stats": {
          "comparisons": 5,
          "swaps": 4
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c3-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 1
          },
          {
            "name": "j",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 2,
          "left": 6,
          "right": 1
        },
        "explanation": "6 is bigger than 1, and they're 2 apart. Swap them — 1 jumps 2 places left in one go.",
        "stats": {
          "comparisons": 6,
          "swaps": 4
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c3-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 3,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "gap": 2,
          "moved": 1,
          "swaps": 5
        },
        "explanation": "1 leaps 2 places left. Insertion sort would have needed 2 separate steps for that.",
        "stats": {
          "comparisons": 6,
          "swaps": 5
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c3-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 2
          },
          {
            "name": "j",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 2,
          "left": 5,
          "right": 9
        },
        "explanation": "5 is not bigger than 9. This pair is fine.",
        "stats": {
          "comparisons": 7,
          "swaps": 5
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c3-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 3
          },
          {
            "name": "j",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 2,
          "left": 6,
          "right": 8
        },
        "explanation": "6 is not bigger than 8. This pair is fine.",
        "stats": {
          "comparisons": 8,
          "swaps": 5
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c3-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 4
          },
          {
            "name": "j",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 2,
          "left": 9,
          "right": 4
        },
        "explanation": "9 is bigger than 4, and they're 2 apart. Swap them — 4 jumps 2 places left in one go.",
        "stats": {
          "comparisons": 9,
          "swaps": 5
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c3-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 4,
            "role": "swap"
          },
          {
            "index": 6,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "gap": 2,
          "moved": 4,
          "swaps": 6
        },
        "explanation": "4 leaps 2 places left. Insertion sort would have needed 2 separate steps for that.",
        "stats": {
          "comparisons": 9,
          "swaps": 6
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c3-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 2
          },
          {
            "name": "j",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 2,
          "left": 5,
          "right": 4
        },
        "explanation": "5 is bigger than 4, and they're 2 apart. Swap them — 4 jumps 2 places left in one go.",
        "stats": {
          "comparisons": 10,
          "swaps": 6
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c3-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 4,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "gap": 2,
          "moved": 4,
          "swaps": 7
        },
        "explanation": "4 leaps 2 places left. Insertion sort would have needed 2 separate steps for that.",
        "stats": {
          "comparisons": 10,
          "swaps": 7
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c3-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 0
          },
          {
            "name": "j",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 2,
          "left": 3,
          "right": 4
        },
        "explanation": "3 is not bigger than 4. This pair is fine.",
        "stats": {
          "comparisons": 11,
          "swaps": 7
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c3-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 5
          },
          {
            "name": "j",
            "index": 7
          }
        ],
        "highlights": [
          {
            "index": 5,
            "role": "compare"
          },
          {
            "index": 7,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 2,
          "left": 8,
          "right": 7
        },
        "explanation": "8 is bigger than 7, and they're 2 apart. Swap them — 7 jumps 2 places left in one go.",
        "stats": {
          "comparisons": 12,
          "swaps": 7
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 5,
            "role": "swap"
          },
          {
            "index": 7,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "gap": 2,
          "moved": 7,
          "swaps": 8
        },
        "explanation": "7 leaps 2 places left. Insertion sort would have needed 2 separate steps for that.",
        "stats": {
          "comparisons": 12,
          "swaps": 8
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 3
          },
          {
            "name": "j",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 2,
          "left": 6,
          "right": 7
        },
        "explanation": "6 is not bigger than 7. This pair is fine.",
        "stats": {
          "comparisons": 13,
          "swaps": 8
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "pivot"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 2,
            "role": "pivot"
          },
          {
            "index": 3,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "pivot"
          },
          {
            "index": 6,
            "role": "pivot"
          },
          {
            "index": 7,
            "role": "pivot"
          }
        ],
        "codeLine": 3,
        "variables": {
          "gap": 1
        },
        "explanation": "New pass with gap 1: only compare values 1 apart. Everything 1 apart gets put in order among itself.",
        "stats": {
          "comparisons": 13,
          "swaps": 8
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 0
          },
          {
            "name": "j",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 1,
          "left": 3,
          "right": 1
        },
        "explanation": "3 is bigger than 1, and they're 1 apart. Swap them — 1 jumps 1 places left in one go.",
        "stats": {
          "comparisons": 14,
          "swaps": 8
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          },
          {
            "index": 1,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "gap": 1,
          "moved": 1,
          "swaps": 9
        },
        "explanation": "1 leaps 1 places left. Insertion sort would have needed 1 separate steps for that.",
        "stats": {
          "comparisons": 14,
          "swaps": 9
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 1
          },
          {
            "name": "j",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 1,
          "left": 3,
          "right": 4
        },
        "explanation": "3 is not bigger than 4. This pair is fine.",
        "stats": {
          "comparisons": 15,
          "swaps": 9
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 2
          },
          {
            "name": "j",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 1,
          "left": 4,
          "right": 6
        },
        "explanation": "4 is not bigger than 6. This pair is fine.",
        "stats": {
          "comparisons": 16,
          "swaps": 9
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 3
          },
          {
            "name": "j",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 1,
          "left": 6,
          "right": 5
        },
        "explanation": "6 is bigger than 5, and they're 1 apart. Swap them — 5 jumps 1 places left in one go.",
        "stats": {
          "comparisons": 17,
          "swaps": 9
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 3,
            "role": "swap"
          },
          {
            "index": 4,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "gap": 1,
          "moved": 5,
          "swaps": 10
        },
        "explanation": "5 leaps 1 places left. Insertion sort would have needed 1 separate steps for that.",
        "stats": {
          "comparisons": 17,
          "swaps": 10
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 2
          },
          {
            "name": "j",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 1,
          "left": 4,
          "right": 5
        },
        "explanation": "4 is not bigger than 5. This pair is fine.",
        "stats": {
          "comparisons": 18,
          "swaps": 10
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 4
          },
          {
            "name": "j",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 1,
          "left": 6,
          "right": 7
        },
        "explanation": "6 is not bigger than 7. This pair is fine.",
        "stats": {
          "comparisons": 19,
          "swaps": 10
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 5
          },
          {
            "name": "j",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 5,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 1,
          "left": 7,
          "right": 9
        },
        "explanation": "7 is not bigger than 9. This pair is fine.",
        "stats": {
          "comparisons": 20,
          "swaps": 10
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c0-9",
            "value": 9
          },
          {
            "id": "c1-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 6
          },
          {
            "name": "j",
            "index": 7
          }
        ],
        "highlights": [
          {
            "index": 6,
            "role": "compare"
          },
          {
            "index": 7,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 1,
          "left": 9,
          "right": 8
        },
        "explanation": "9 is bigger than 8, and they're 1 apart. Swap them — 8 jumps 1 places left in one go.",
        "stats": {
          "comparisons": 21,
          "swaps": 10
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c0-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 6,
            "role": "swap"
          },
          {
            "index": 7,
            "role": "swap"
          }
        ],
        "codeLine": 7,
        "variables": {
          "gap": 1,
          "moved": 8,
          "swaps": 11
        },
        "explanation": "8 leaps 1 places left. Insertion sort would have needed 1 separate steps for that.",
        "stats": {
          "comparisons": 21,
          "swaps": 11
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c0-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j-gap",
            "index": 5
          },
          {
            "name": "j",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 5,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "compare"
          }
        ],
        "codeLine": 6,
        "variables": {
          "gap": 1,
          "left": 7,
          "right": 8
        },
        "explanation": "7 is not bigger than 8. This pair is fine.",
        "stats": {
          "comparisons": 22,
          "swaps": 11
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c7-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-4",
            "value": 4
          },
          {
            "id": "c4-5",
            "value": 5
          },
          {
            "id": "c5-6",
            "value": 6
          },
          {
            "id": "c3-7",
            "value": 7
          },
          {
            "id": "c1-8",
            "value": 8
          },
          {
            "id": "c0-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"
          },
          {
            "index": 7,
            "role": "sorted"
          }
        ],
        "codeLine": 12,
        "variables": {
          "comparisons": 22,
          "swaps": 11
        },
        "explanation": "Done in 22 comparisons. The final gap of 1 is just an insertion sort — but by then the list is nearly sorted, which is insertion sort's best case.",
        "stats": {
          "comparisons": 22,
          "swaps": 11
        },
        "accent": "sorted"
      }
    ]
  }
}