{
  "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": [
          "the native tests did not run in this build",
          "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": [
          "the native tests fail",
          "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": [
          "the native tests did not run in this build",
          "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": 1387
      },
      "cpp": {
        "slug": "binary-search",
        "lang": "cpp",
        "pass": true,
        "output": "ok",
        "ms": 5889
      },
      "go": {
        "slug": "binary-search",
        "lang": "go",
        "pass": true,
        "output": "ok  \texample.com/impl\t0.348s",
        "ms": 2947
      },
      "java": {
        "slug": "binary-search",
        "lang": "java",
        "pass": false,
        "output": "The operation couldn’t be completed. Unable to locate a Java Runtime.\nPlease visit http://www.java.com for information on installing Java.",
        "ms": 772
      },
      "javascript": {
        "slug": "binary-search",
        "lang": "javascript",
        "pass": true,
        "output": "✔ examples (11.000583ms)\n✔ edges (0.743667ms)\n✔ random against a linear scan (28.753333ms)\nℹ tests 3\nℹ suites 0\nℹ pass 3\nℹ fail 0\nℹ cancelled 0\nℹ skipped 0\nℹ todo 0\nℹ duration_ms 899.108542",
        "ms": 1906
      },
      "python": {
        "slug": "binary-search",
        "lang": "python",
        "pass": true,
        "output": ".....                                                                    [100%]\n5 passed in 0.60s",
        "ms": 2637
      },
      "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.02s\n\n\nrunning 0 tests\n\ntest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s",
        "ms": 3302
      },
      "typescript": {
        "slug": "binary-search",
        "lang": "typescript",
        "pass": true,
        "output": "✔ examples (1.899541ms)\n✔ edges (0.203375ms)\n✔ random against a linear scan (10.870584ms)\nℹ tests 3\nℹ suites 0\nℹ pass 3\nℹ fail 0\nℹ cancelled 0\nℹ skipped 0\nℹ todo 0\nℹ duration_ms 453.7305",
        "ms": 619
      }
    }
  },
  "langs": [
    "c",
    "cpp",
    "csharp",
    "go",
    "java",
    "javascript",
    "kotlin",
    "python",
    "rust",
    "typescript"
  ],
  "proofs": [
    "correctness"
  ],
  "scene": true
}
