{
  "tag": "2B7QDX",
  "kind": "algorithm",
  "slug": "binary-search",
  "name": "Binary search",
  "aliases": [
    "half-interval search",
    "logarithmic search",
    "bisection search"
  ],
  "area": "searching/sequential-and-binary-search",
  "level": "intro",
  "summary": "Find a key in a sorted array. Compare the key with the middle item, and keep only the half that can hold the key.",
  "solves": [
    {
      "problem": "search-in-sorted-array"
    }
  ],
  "model": "comparison-model",
  "complexity": {
    "vars": {
      "n": "the number of items in the array"
    },
    "time": {
      "average": "Θ(log n)",
      "best": "Θ(1)",
      "worst": "Θ(log n)"
    },
    "space": {
      "extra": "Θ(1)"
    }
  },
  "properties": {
    "comparison": true,
    "deterministic": true,
    "recursive": false
  },
  "history": {
    "year": 1946,
    "people": [
      "John Mauchly",
      "Derrick Henry Lehmer"
    ],
    "note": "Mauchly described the method in the Moore School Lectures in 1946. Lehmer gave a version for any n in 1960."
  },
  "prereqs": [
    "loop-invariant",
    "asymptotic-notation"
  ],
  "sources": [
    {
      "book": "isbn:9780201896855",
      "title": "Donald E. Knuth, The Art of Computer Programming, Volume 3, second edition",
      "locator": "Section 6.2.1, Algorithm B"
    },
    {
      "book": "isbn:9780262046305",
      "title": "Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition",
      "locator": "Exercise 2.3-6"
    },
    {
      "url": "https://research.google/blog/extra-extra-read-all-about-it-nearly-all-binary-searches-and-mergesorts-are-broken/",
      "title": "Joshua Bloch, Nearly all binary searches and mergesorts are broken (2006)",
      "note": "the overflow of (lo + hi) / 2"
    }
  ],
  "see_also": [
    "insertion-sort",
    "merge-sort"
  ],
  "url": "https://algorithm.tamnd.com/binary-search/",
  "path": "nodes/searching/sequential-and-binary-search/binary-search",
  "status": {
    "slug": "binary-search",
    "h": {
      "level": 1,
      "max": 3,
      "label": "",
      "name": "",
      "next": [
        "a full proof of time.average",
        "a full proof of time.best",
        "a full proof of time.worst",
        "a full proof of space.extra"
      ]
    },
    "l": {
      "level": 0,
      "max": 4,
      "label": "",
      "name": "",
      "next": [
        "name a Lean module in the lean field (07)"
      ]
    },
    "i": {
      "c": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "cpp": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "csharp": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "go": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "java": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "javascript": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "kotlin": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "python": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "rust": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      },
      "typescript": {
        "level": 0,
        "max": 4,
        "label": "",
        "name": "",
        "next": [
          "I1 needs the shared cases of the contract and the JSON adapter (09.2), which are M0 work"
        ]
      }
    },
    "v": {
      "level": 1,
      "max": 3,
      "label": "",
      "name": "",
      "next": [
        "V2 needs the oracle check V03, line-trace agreement V06, the counter check V07 and the budgets V08 (10.8.2)"
      ]
    },
    "w": {
      "level": 0,
      "max": 5,
      "label": "",
      "name": "",
      "next": [
        "the lead paragraph"
      ]
    },
    "tests": {
      "c": {
        "slug": "binary-search",
        "lang": "c",
        "pass": true,
        "output": "ok",
        "ms": 916
      },
      "cpp": {
        "slug": "binary-search",
        "lang": "cpp",
        "pass": true,
        "output": "ok",
        "ms": 2289
      },
      "csharp": {
        "slug": "binary-search",
        "lang": "csharp",
        "pass": true,
        "output": "Test run for /tmp/algo-test-csharp-3862806794/bin/Debug/net10.0/Impl.Tests.dll (.NETCoreApp,Version=v10.0)\nA total of 1 test files matched the specified pattern.\n\nPassed!  - Failed:     0, Passed:     8, Skipped:     0, Total:     8, Duration: 38 ms - Impl.Tests.dll (net10.0)",
        "ms": 7667
      },
      "go": {
        "slug": "binary-search",
        "lang": "go",
        "pass": true,
        "output": "ok  \texample.com/impl\t0.003s",
        "ms": 2924
      },
      "java": {
        "slug": "binary-search",
        "lang": "java",
        "pass": true,
        "output": "Test run finished after 113 ms\n[         4 containers found      ]\n[         0 containers skipped    ]\n[         4 containers started    ]\n[         0 containers aborted    ]\n[         4 containers successful ]\n[         0 containers failed     ]\n[         3 tests found           ]\n[         0 tests skipped         ]\n[         3 tests started         ]\n[         0 tests aborted         ]\n[         3 tests successful      ]\n[         0 tests failed          ]",
        "ms": 2265
      },
      "javascript": {
        "slug": "binary-search",
        "lang": "javascript",
        "pass": true,
        "output": "✔ examples (0.971751ms)\n✔ edges (0.174497ms)\n✔ random against a linear scan (13.009074ms)\nℹ tests 3\nℹ suites 0\nℹ pass 3\nℹ fail 0\nℹ cancelled 0\nℹ skipped 0\nℹ todo 0\nℹ duration_ms 82.887217",
        "ms": 123
      },
      "kotlin": {
        "slug": "binary-search",
        "lang": "kotlin",
        "pass": true,
        "output": "Test run finished after 113 ms\n[         4 containers found      ]\n[         0 containers skipped    ]\n[         4 containers started    ]\n[         0 containers aborted    ]\n[         4 containers successful ]\n[         0 containers failed     ]\n[         3 tests found           ]\n[         0 tests skipped         ]\n[         3 tests started         ]\n[         0 tests aborted         ]\n[         3 tests successful      ]\n[         0 tests failed          ]",
        "ms": 5448
      },
      "python": {
        "slug": "binary-search",
        "lang": "python",
        "pass": true,
        "output": ".....                                                                    [100%]\n5 passed in 0.03s",
        "ms": 1107
      },
      "rust": {
        "slug": "binary-search",
        "lang": "rust",
        "pass": true,
        "output": "running 0 tests\n\ntest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s\n\n\nrunning 3 tests\n...\ntest result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s\n\n\nrunning 0 tests\n\ntest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.02s",
        "ms": 21824
      },
      "typescript": {
        "slug": "binary-search",
        "lang": "typescript",
        "pass": true,
        "output": "✔ examples (0.926634ms)\n✔ edges (0.206327ms)\n✔ random against a linear scan (7.950472ms)\nℹ tests 3\nℹ suites 0\nℹ pass 3\nℹ fail 0\nℹ cancelled 0\nℹ skipped 0\nℹ todo 0\nℹ duration_ms 119.843679",
        "ms": 158
      }
    }
  },
  "langs": [
    "c",
    "cpp",
    "csharp",
    "go",
    "java",
    "javascript",
    "kotlin",
    "python",
    "rust",
    "typescript"
  ],
  "proofs": [
    "correctness"
  ],
  "scene": true
}
