{
  "id": "sorting/quick-sort",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/sorting/quick-sort",
  "topic": {
    "slug": "sorting",
    "title": "Sorting"
  },
  "title": "Quick Sort",
  "tagline": "Pick a pivot, split around it, and let the halves sort themselves.",
  "vizKind": "bars",
  "complexity": {
    "time": {
      "best": "O(n log n)",
      "average": "O(n log n)",
      "worst": "O(n²)"
    },
    "space": "O(log n)",
    "growth": "nlogn",
    "note": "Average case splits the list roughly in half each time: n log n. Worst case — a pivot that's always the smallest or largest value — splits off just one item at a time and collapses to n². Good pivot choice is what keeps it fast in practice."
  },
  "code": {
    "javascript": "function quickSort(a, lo = 0, hi = a.length - 1) {\n  if (lo >= hi) return a;\n  const p = partition(a, lo, hi);\n  quickSort(a, lo, p - 1);\n  quickSort(a, p + 1, hi);\n  return a;\n}\n\nfunction partition(a, lo, hi) {\n  const pivot = a[hi];\n  let i = lo;\n  for (let j = lo; j < hi; j++) {\n    if (a[j] < pivot) {\n      [a[i], a[j]] = [a[j], a[i]];\n      i++;\n    }\n  }\n  [a[i], a[hi]] = [a[hi], a[i]];\n  return i;\n}",
    "python": "def quick_sort(a, lo=0, hi=None):\n    if hi is None: hi = len(a) - 1\n    if lo >= hi: return a\n    p = partition(a, lo, hi)\n    quick_sort(a, lo, p - 1)\n    quick_sort(a, p + 1, hi)\n    return a\n\n\ndef partition(a, lo, hi):\n    pivot = a[hi]\n    i = lo\n    for j in range(lo, hi):\n        if a[j] < pivot:\n            a[i], a[j] = a[j], a[i]\n            i += 1\n\n    a[i], a[hi] = a[hi], a[i]\n    return i\n",
    "java": "void quickSort(int[] a, int lo, int hi) {\n  if (lo >= hi) return;\n  int p = partition(a, lo, hi);\n  quickSort(a, lo, p - 1);\n  quickSort(a, p + 1, hi);\n}\n\n\nint partition(int[] a, int lo, int hi) {\n  int pivot = a[hi];\n  int i = lo;\n  for (int j = lo; j < hi; j++) {\n    if (a[j] < pivot) {\n      int t = a[i]; a[i] = a[j]; a[j] = t;\n      i++;\n    }\n  }\n  int t = a[i]; a[i] = a[hi]; a[hi] = t;\n  return i;\n}",
    "cpp": "void quickSort(vector<int>& a, int lo, int hi) {\n  if (lo >= hi) return;\n  int p = partition(a, lo, hi);\n  quickSort(a, lo, p - 1);\n  quickSort(a, p + 1, hi);\n}\n\n\nint partition(vector<int>& a, int lo, int hi) {\n  int pivot = a[hi];\n  int i = lo;\n  for (int j = lo; j < hi; j++) {\n    if (a[j] < pivot) {\n      swap(a[i], a[j]);\n      i++;\n    }\n  }\n  swap(a[i], a[hi]);\n  return i;\n}"
  },
  "defaultInput": [
    8,
    3,
    5,
    1,
    9,
    2,
    7,
    4
  ],
  "defaultTarget": null,
  "inputHint": "The pink bar is the pivot. Watch smaller values jump to its left.",
  "pitfalls": [
    {
      "wrong": "Always choosing the first or last element as the pivot.",
      "right": "On an already-sorted list that's the worst possible pivot every time, giving O(n²). Pick a random element, or the median of three, to stay fast."
    },
    {
      "wrong": "Forgetting to put the pivot in its final place after partitioning.",
      "right": "After the loop, swap the pivot into the boundary position. Skip that and the pivot never actually lands where it belongs."
    },
    {
      "wrong": "Recursing on ranges that include the already-placed pivot.",
      "right": "The pivot is done — recurse on lo..p−1 and p+1..hi, never on p itself, or you'll loop forever."
    },
    {
      "wrong": "Believing quick sort is always faster than merge sort.",
      "right": "On average it usually is, and it uses less memory. But merge sort's worst case is still n log n; quick sort's is n². Which is 'better' depends on whether you can tolerate a bad day."
    }
  ],
  "quiz": [
    {
      "q": "After one partition step, what is true about the pivot?",
      "options": [
        "It's at the front of the list",
        "It's in its final sorted position",
        "It's the largest value",
        "Nothing yet — sorting happens later"
      ],
      "answer": 1,
      "why": "Everything smaller is left of it and everything bigger is right of it, so the pivot is exactly where it belongs in the finished list. It never moves again."
    },
    {
      "q": "When does quick sort degrade to O(n²)?",
      "options": [
        "On very large lists",
        "When the pivot is consistently the smallest or largest value",
        "When there are duplicates",
        "It never does"
      ],
      "answer": 1,
      "why": "A bad pivot peels off just one element per pass instead of halving, so you get n levels of n work — n². Random or median-of-three pivots make this vanishingly unlikely."
    },
    {
      "q": "What does quick sort have that merge sort doesn't?",
      "options": [
        "A guaranteed n log n worst case",
        "In-place sorting with no second array",
        "Stability",
        "It works on unsorted data"
      ],
      "answer": 1,
      "why": "Quick sort partitions in place, needing only O(log n) stack space. Merge sort needs a full extra array of size n. That memory saving is a big reason libraries favour quick sort."
    }
  ],
  "problems": [
    {
      "name": "Kth Largest Element in an Array",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/kth-largest-element-in-an-array/"
    },
    {
      "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": 33,
    "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
          },
          {
            "id": "c7-4",
            "value": 4
          }
        ],
        "pointers": [],
        "highlights": [],
        "codeLine": 1,
        "variables": {},
        "explanation": "Quick sort picks one value as a pivot, then shoves everything smaller to its left and everything bigger to its right. The pivot lands in its final home — then it does the same to each side.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "pivot"
      },
      {
        "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
          },
          {
            "id": "c7-4",
            "value": 4
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 0,
            "role": "active"
          },
          {
            "index": 1,
            "role": "active"
          },
          {
            "index": 2,
            "role": "active"
          },
          {
            "index": 3,
            "role": "active"
          },
          {
            "index": 4,
            "role": "active"
          },
          {
            "index": 5,
            "role": "active"
          },
          {
            "index": 6,
            "role": "active"
          }
        ],
        "codeLine": 10,
        "variables": {
          "lo": 0,
          "hi": 7,
          "pivot": 4
        },
        "explanation": "Take the last value in this section, 4, as the pivot. Now walk the section and pull every smaller value to the left.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "pivot"
      },
      {
        "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
          },
          {
            "id": "c7-4",
            "value": 4
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 0
          },
          {
            "name": "j",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 0,
            "role": "compare"
          }
        ],
        "codeLine": 13,
        "variables": {
          "pivot": 4,
          "looking_at": 8
        },
        "explanation": "8 is not smaller than 4 — leave it on the right for now.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "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
          },
          {
            "id": "c7-4",
            "value": 4
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 0
          },
          {
            "name": "j",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 1,
            "role": "compare"
          }
        ],
        "codeLine": 13,
        "variables": {
          "pivot": 4,
          "looking_at": 3
        },
        "explanation": "3 is smaller than the pivot 4 — it belongs on the left.",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "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
          },
          {
            "id": "c7-4",
            "value": 4
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 0
          },
          {
            "name": "j",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 0,
            "role": "swap"
          },
          {
            "index": 1,
            "role": "swap"
          }
        ],
        "codeLine": 14,
        "variables": {
          "pivot": 4
        },
        "explanation": "Swap it into the left group, at position 0.",
        "stats": {
          "comparisons": 2,
          "swaps": 1
        },
        "accent": "swap"
      },
      {
        "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
          },
          {
            "id": "c7-4",
            "value": 4
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 1
          },
          {
            "name": "j",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 2,
            "role": "compare"
          }
        ],
        "codeLine": 13,
        "variables": {
          "pivot": 4,
          "looking_at": 5
        },
        "explanation": "5 is not smaller than 4 — leave it on the right for now.",
        "stats": {
          "comparisons": 3,
          "swaps": 1
        },
        "accent": "compare"
      },
      {
        "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
          },
          {
            "id": "c7-4",
            "value": 4
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 1
          },
          {
            "name": "j",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 3,
            "role": "compare"
          }
        ],
        "codeLine": 13,
        "variables": {
          "pivot": 4,
          "looking_at": 1
        },
        "explanation": "1 is smaller than the pivot 4 — it belongs on the left.",
        "stats": {
          "comparisons": 4,
          "swaps": 1
        },
        "accent": "compare"
      },
      {
        "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
          },
          {
            "id": "c7-4",
            "value": 4
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 1
          },
          {
            "name": "j",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 3,
            "role": "swap"
          }
        ],
        "codeLine": 14,
        "variables": {
          "pivot": 4
        },
        "explanation": "Swap it into the left group, at position 1.",
        "stats": {
          "comparisons": 4,
          "swaps": 2
        },
        "accent": "swap"
      },
      {
        "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
          },
          {
            "id": "c7-4",
            "value": 4
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 2
          },
          {
            "name": "j",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "compare"
          }
        ],
        "codeLine": 13,
        "variables": {
          "pivot": 4,
          "looking_at": 9
        },
        "explanation": "9 is not smaller than 4 — leave it on the right for now.",
        "stats": {
          "comparisons": 5,
          "swaps": 2
        },
        "accent": "compare"
      },
      {
        "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
          },
          {
            "id": "c7-4",
            "value": 4
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 2
          },
          {
            "name": "j",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "compare"
          }
        ],
        "codeLine": 13,
        "variables": {
          "pivot": 4,
          "looking_at": 2
        },
        "explanation": "2 is smaller than the pivot 4 — it belongs on the left.",
        "stats": {
          "comparisons": 6,
          "swaps": 2
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c7-4",
            "value": 4
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 2
          },
          {
            "name": "j",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 5,
            "role": "swap"
          }
        ],
        "codeLine": 14,
        "variables": {
          "pivot": 4
        },
        "explanation": "Swap it into the left group, at position 2.",
        "stats": {
          "comparisons": 6,
          "swaps": 3
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c7-4",
            "value": 4
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 3
          },
          {
            "name": "j",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 6,
            "role": "compare"
          }
        ],
        "codeLine": 13,
        "variables": {
          "pivot": 4,
          "looking_at": 7
        },
        "explanation": "7 is not smaller than 4 — leave it on the right for now.",
        "stats": {
          "comparisons": 7,
          "swaps": 3
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 18,
        "variables": {
          "pivotFinalIndex": 3
        },
        "explanation": "Drop the pivot 4 into the gap at position 3. Everything left of it is smaller, everything right is bigger — so 4 is now in its final place, for good.",
        "stats": {
          "comparisons": 7,
          "swaps": 4
        },
        "accent": "sorted"
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 2,
            "role": "pivot"
          },
          {
            "index": 0,
            "role": "active"
          },
          {
            "index": 1,
            "role": "active"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 10,
        "variables": {
          "lo": 0,
          "hi": 2,
          "pivot": 2
        },
        "explanation": "Take the last value in this section, 2, as the pivot. Now walk the section and pull every smaller value to the left.",
        "stats": {
          "comparisons": 7,
          "swaps": 4
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 0
          },
          {
            "name": "j",
            "index": 0
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "pivot"
          },
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 13,
        "variables": {
          "pivot": 2,
          "looking_at": 3
        },
        "explanation": "3 is not smaller than 2 — leave it on the right for now.",
        "stats": {
          "comparisons": 8,
          "swaps": 4
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 0
          },
          {
            "name": "j",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "pivot"
          },
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 13,
        "variables": {
          "pivot": 2,
          "looking_at": 1
        },
        "explanation": "1 is smaller than the pivot 2 — it belongs on the left.",
        "stats": {
          "comparisons": 9,
          "swaps": 4
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 0
          },
          {
            "name": "j",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "pivot"
          },
          {
            "index": 0,
            "role": "swap"
          },
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 14,
        "variables": {
          "pivot": 2
        },
        "explanation": "Swap it into the left group, at position 0.",
        "stats": {
          "comparisons": 9,
          "swaps": 5
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          }
        ],
        "codeLine": 18,
        "variables": {
          "pivotFinalIndex": 1
        },
        "explanation": "Drop the pivot 2 into the gap at position 1. Everything left of it is smaller, everything right is bigger — so 2 is now in its final place, for good.",
        "stats": {
          "comparisons": 9,
          "swaps": 6
        },
        "accent": "sorted"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "lo": 0,
          "hi": 0
        },
        "explanation": "A section of one item (1) is already in place.",
        "stats": {
          "comparisons": 9,
          "swaps": 6
        },
        "accent": "sorted"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "lo": 2,
          "hi": 2
        },
        "explanation": "A section of one item (3) is already in place.",
        "stats": {
          "comparisons": 9,
          "swaps": 6
        },
        "accent": "sorted"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "active"
          },
          {
            "index": 5,
            "role": "active"
          },
          {
            "index": 6,
            "role": "active"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 10,
        "variables": {
          "lo": 4,
          "hi": 7,
          "pivot": 8
        },
        "explanation": "Take the last value in this section, 8, as the pivot. Now walk the section and pull every smaller value to the left.",
        "stats": {
          "comparisons": 9,
          "swaps": 6
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 4
          },
          {
            "name": "j",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 13,
        "variables": {
          "pivot": 8,
          "looking_at": 9
        },
        "explanation": "9 is not smaller than 8 — leave it on the right for now.",
        "stats": {
          "comparisons": 10,
          "swaps": 6
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 4
          },
          {
            "name": "j",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 13,
        "variables": {
          "pivot": 8,
          "looking_at": 5
        },
        "explanation": "5 is smaller than the pivot 8 — it belongs on the left.",
        "stats": {
          "comparisons": 11,
          "swaps": 6
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 4
          },
          {
            "name": "j",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "swap"
          },
          {
            "index": 5,
            "role": "swap"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 14,
        "variables": {
          "pivot": 8
        },
        "explanation": "Swap it into the left group, at position 4.",
        "stats": {
          "comparisons": 11,
          "swaps": 7
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 5
          },
          {
            "name": "j",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 6,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 13,
        "variables": {
          "pivot": 8,
          "looking_at": 7
        },
        "explanation": "7 is smaller than the pivot 8 — it belongs on the left.",
        "stats": {
          "comparisons": 12,
          "swaps": 7
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c0-8",
            "value": 8
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 5
          },
          {
            "name": "j",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 7,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "swap"
          },
          {
            "index": 6,
            "role": "swap"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 14,
        "variables": {
          "pivot": 8
        },
        "explanation": "Swap it into the left group, at position 5.",
        "stats": {
          "comparisons": 12,
          "swaps": 8
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 6,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 18,
        "variables": {
          "pivotFinalIndex": 6
        },
        "explanation": "Drop the pivot 8 into the gap at position 6. Everything left of it is smaller, everything right is bigger — so 8 is now in its final place, for good.",
        "stats": {
          "comparisons": 12,
          "swaps": 9
        },
        "accent": "sorted"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 5,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "active"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 10,
        "variables": {
          "lo": 4,
          "hi": 5,
          "pivot": 7
        },
        "explanation": "Take the last value in this section, 7, as the pivot. Now walk the section and pull every smaller value to the left.",
        "stats": {
          "comparisons": 12,
          "swaps": 9
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "i",
            "index": 4
          },
          {
            "name": "j",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 5,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 13,
        "variables": {
          "pivot": 7,
          "looking_at": 5
        },
        "explanation": "5 is smaller than the pivot 7 — it belongs on the left.",
        "stats": {
          "comparisons": 13,
          "swaps": 9
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          }
        ],
        "codeLine": 18,
        "variables": {
          "pivotFinalIndex": 5
        },
        "explanation": "Drop the pivot 7 into the gap at position 5. Everything left of it is smaller, everything right is bigger — so 7 is now in its final place, for good.",
        "stats": {
          "comparisons": 13,
          "swaps": 9
        },
        "accent": "sorted"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "lo": 4,
          "hi": 4
        },
        "explanation": "A section of one item (5) is already in place.",
        "stats": {
          "comparisons": 13,
          "swaps": 9
        },
        "accent": "sorted"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 7,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 7,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "lo": 7,
          "hi": 7
        },
        "explanation": "A section of one item (9) is already in place.",
        "stats": {
          "comparisons": 13,
          "swaps": 9
        },
        "accent": "sorted"
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c7-4",
            "value": 4
          },
          {
            "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"
          },
          {
            "index": 7,
            "role": "sorted"
          }
        ],
        "codeLine": 6,
        "variables": {
          "comparisons": 13,
          "swaps": 9
        },
        "explanation": "Done in 13 comparisons. On average quick sort is n log n, and it does its shuffling in place — no second array like merge sort needs.",
        "stats": {
          "comparisons": 13,
          "swaps": 9
        },
        "accent": "sorted"
      }
    ]
  }
}