{
  "slug": "linear-search",
  "title": "Linear Search",
  "category": "Searching",
  "difficulty": "intro",
  "complexity": {
    "time": "O(n)",
    "space": "O(1)"
  },
  "idea": "Look at every element in turn and compare it with the key. Simple, works on any array — sorted or not.",
  "code": [
    "for i in 0 .. n-1:",
    "    if a[i] == key: return i",
    "return -1"
  ],
  "example": {
    "array": [
      11,
      3,
      7,
      15,
      9,
      2,
      20
    ],
    "target": 15
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 5,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "Scan left to right, looking for 15.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        11,
        3,
        7,
        15,
        9,
        2,
        20
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": []
    },
    {
      "i": 1,
      "op": "read",
      "caption": "a[0] = 11 ≠ 15. Keep going.",
      "note": null,
      "codeLine": 2,
      "stats": {
        "checked": 1
      },
      "array": [
        11,
        3,
        7,
        15,
        9,
        2,
        20
      ],
      "focus": [
        "read",
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 0,
          "tone": "read"
        }
      ],
      "regions": []
    },
    {
      "i": 2,
      "op": "read",
      "caption": "a[1] = 3 ≠ 15. Keep going.",
      "note": null,
      "codeLine": 2,
      "stats": {
        "checked": 2
      },
      "array": [
        11,
        3,
        7,
        15,
        9,
        2,
        20
      ],
      "focus": [
        null,
        "read",
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 1,
          "tone": "read"
        }
      ],
      "regions": []
    },
    {
      "i": 3,
      "op": "read",
      "caption": "a[2] = 7 ≠ 15. Keep going.",
      "note": null,
      "codeLine": 2,
      "stats": {
        "checked": 3
      },
      "array": [
        11,
        3,
        7,
        15,
        9,
        2,
        20
      ],
      "focus": [
        null,
        null,
        "read",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 2,
          "tone": "read"
        }
      ],
      "regions": []
    },
    {
      "i": 4,
      "op": "found",
      "caption": "a[3] = 15 — that's 15. Found it at index 3.",
      "note": null,
      "codeLine": 2,
      "stats": {
        "checked": 4
      },
      "array": [
        11,
        3,
        7,
        15,
        9,
        2,
        20
      ],
      "focus": [
        null,
        null,
        null,
        "target",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "i",
          "index": 3,
          "tone": "read"
        }
      ],
      "regions": []
    }
  ]
}