{
  "slug": "dutch-national-flag",
  "title": "Dutch National Flag",
  "category": "Arrays",
  "difficulty": "medium",
  "complexity": {
    "time": "O(n)",
    "space": "O(1)"
  },
  "idea": "With values limited to 0, 1 and 2, keep settled 0s on the left and settled 2s on the right, sorting the middle. One pass with low/mid/high pointers places every value.",
  "code": [
    "low = mid = 0; high = n-1",
    "while mid <= high:",
    "    if a[mid]==0: swap(low,mid); low++; mid++",
    "    elif a[mid]==1: mid++",
    "    else: swap(mid,high); high--"
  ],
  "example": {
    "array": [
      2,
      0,
      2,
      1,
      1,
      0,
      1,
      2,
      0
    ]
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 11,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "Values are only 0, 1 or 2. Three pointers: low, mid, high.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        2,
        0,
        2,
        1,
        1,
        0,
        1,
        2,
        0
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": []
    },
    {
      "i": 1,
      "op": "swap",
      "caption": "a[mid] = 2 → send it to the right block.",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "array": [
        0,
        0,
        2,
        1,
        1,
        0,
        1,
        2,
        2
      ],
      "focus": [
        "swap",
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        "swap"
      ],
      "markers": [
        {
          "name": "low",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "mid",
          "index": 0,
          "tone": "compare"
        },
        {
          "name": "high",
          "index": 8,
          "tone": "bound"
        }
      ],
      "regions": []
    },
    {
      "i": 2,
      "op": "swap",
      "caption": "a[mid] = 0 → send it to the left block.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        0,
        0,
        2,
        1,
        1,
        0,
        1,
        2,
        2
      ],
      "focus": [
        "swap",
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "low",
          "index": 0,
          "tone": "bound"
        },
        {
          "name": "mid",
          "index": 0,
          "tone": "compare"
        },
        {
          "name": "high",
          "index": 7,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "twos",
          "from": 8,
          "to": 8,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 3,
      "op": "swap",
      "caption": "a[mid] = 0 → send it to the left block.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        0,
        0,
        2,
        1,
        1,
        0,
        1,
        2,
        2
      ],
      "focus": [
        null,
        "swap",
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "low",
          "index": 1,
          "tone": "bound"
        },
        {
          "name": "mid",
          "index": 1,
          "tone": "compare"
        },
        {
          "name": "high",
          "index": 7,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "twos",
          "from": 8,
          "to": 8,
          "tone": "settled"
        },
        {
          "label": "zeros",
          "from": 0,
          "to": 0,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 4,
      "op": "swap",
      "caption": "a[mid] = 2 → send it to the right block.",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "array": [
        0,
        0,
        2,
        1,
        1,
        0,
        1,
        2,
        2
      ],
      "focus": [
        null,
        null,
        "swap",
        null,
        null,
        null,
        null,
        "swap",
        null
      ],
      "markers": [
        {
          "name": "low",
          "index": 2,
          "tone": "bound"
        },
        {
          "name": "mid",
          "index": 2,
          "tone": "compare"
        },
        {
          "name": "high",
          "index": 7,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "twos",
          "from": 8,
          "to": 8,
          "tone": "settled"
        },
        {
          "label": "zeros",
          "from": 0,
          "to": 1,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 5,
      "op": "swap",
      "caption": "a[mid] = 2 → send it to the right block.",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "array": [
        0,
        0,
        1,
        1,
        1,
        0,
        2,
        2,
        2
      ],
      "focus": [
        null,
        null,
        "swap",
        null,
        null,
        null,
        "swap",
        null,
        null
      ],
      "markers": [
        {
          "name": "low",
          "index": 2,
          "tone": "bound"
        },
        {
          "name": "mid",
          "index": 2,
          "tone": "compare"
        },
        {
          "name": "high",
          "index": 6,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "twos",
          "from": 7,
          "to": 8,
          "tone": "settled"
        },
        {
          "label": "zeros",
          "from": 0,
          "to": 1,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 6,
      "op": "read",
      "caption": "a[mid] = 1 → already central. Advance mid.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        0,
        0,
        1,
        1,
        1,
        0,
        2,
        2,
        2
      ],
      "focus": [
        null,
        null,
        "read",
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "low",
          "index": 2,
          "tone": "bound"
        },
        {
          "name": "mid",
          "index": 2,
          "tone": "compare"
        },
        {
          "name": "high",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "twos",
          "from": 6,
          "to": 8,
          "tone": "settled"
        },
        {
          "label": "zeros",
          "from": 0,
          "to": 1,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 7,
      "op": "read",
      "caption": "a[mid] = 1 → already central. Advance mid.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        0,
        0,
        1,
        1,
        1,
        0,
        2,
        2,
        2
      ],
      "focus": [
        null,
        null,
        null,
        "read",
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "low",
          "index": 2,
          "tone": "bound"
        },
        {
          "name": "mid",
          "index": 3,
          "tone": "compare"
        },
        {
          "name": "high",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "twos",
          "from": 6,
          "to": 8,
          "tone": "settled"
        },
        {
          "label": "zeros",
          "from": 0,
          "to": 1,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 8,
      "op": "read",
      "caption": "a[mid] = 1 → already central. Advance mid.",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "array": [
        0,
        0,
        1,
        1,
        1,
        0,
        2,
        2,
        2
      ],
      "focus": [
        null,
        null,
        null,
        null,
        "read",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "low",
          "index": 2,
          "tone": "bound"
        },
        {
          "name": "mid",
          "index": 4,
          "tone": "compare"
        },
        {
          "name": "high",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "twos",
          "from": 6,
          "to": 8,
          "tone": "settled"
        },
        {
          "label": "zeros",
          "from": 0,
          "to": 1,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 9,
      "op": "swap",
      "caption": "a[mid] = 0 → send it to the left block.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        0,
        0,
        0,
        1,
        1,
        1,
        2,
        2,
        2
      ],
      "focus": [
        null,
        null,
        "swap",
        null,
        null,
        "swap",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "low",
          "index": 2,
          "tone": "bound"
        },
        {
          "name": "mid",
          "index": 5,
          "tone": "compare"
        },
        {
          "name": "high",
          "index": 5,
          "tone": "bound"
        }
      ],
      "regions": [
        {
          "label": "twos",
          "from": 6,
          "to": 8,
          "tone": "settled"
        },
        {
          "label": "zeros",
          "from": 0,
          "to": 1,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 10,
      "op": "settle",
      "caption": "Sorted into 0s, then 1s, then 2s.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "array": [
        0,
        0,
        0,
        1,
        1,
        1,
        2,
        2,
        2
      ],
      "focus": [
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled"
      ],
      "markers": [],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 8,
          "tone": "settled"
        }
      ]
    }
  ]
}