{"id":633,"date":"2021-06-23T13:14:11","date_gmt":"2021-06-23T12:14:11","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=633"},"modified":"2021-06-24T09:24:02","modified_gmt":"2021-06-24T08:24:02","slug":"binary-search","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/adv-higher\/binary-search\/","title":{"rendered":"Binary Search"},"content":{"rendered":"<p>Linear searching looks through a list\/array item by item until a target value is found, or the end of the list is reached. Linear searches can use unsorted lists, but are very inefficient.<\/p>\n<p>Binary searches only work on sorted lists, with the list repeatedly split into smaller and smaller lists, until the target value is found or the list is empty.<\/p>\n<p>Pointers are used to identify the range of the list to be sorted (low\/high, bottom\/top, left\/right, start\/end).\u00a0An index is chosen between the two pointers, resulting in a match or a value that is too high\/low. In the latter case the range pointers are updated, excluding the index that has just been checked.<\/p>\n<h2>General Algorithm<\/h2>\n<pre>bottom = 0\ntop = listsize - 1\nfound = False\n \nloop while bottom&lt;=top and found = False do\n    middle = int( (top+bottom)\/2 )      #pick a midpoint\n    if list[middle] = target then\n        found = True                    #item found\n    else if list[middle] &gt; target then\n        top = middle - 1                #look in bottom half\n    else\n        bottom = middle + 1             #look in top half\n    end if\nend loop\n \nif found = True then \n    print \"Found at index: \" + middle\nelse\n    print \"Not found\"\nend if\n<\/pre>\n<pre class=\"brush: python; title: Python Function; notranslate\" title=\"Python Function\">\r\n#set pointers to start and end of full list\r\nstart = 0\r\nend = len(array)\r\nfound = False\r\n\r\n# as long as pointers haven't crossed, and target has not been found\r\nwhile (start &lt;= end) and not found:\r\n \r\n    # pick the midpoint between pointers\r\n    middle = (start + end) \/\/ 2\r\n \r\n    # has target been found?\r\n    if target == array&#x5B;middle]:\r\n        found = True\r\n    # is target in lower half of range (start-middle)?\r\n    elif target &lt; array&#x5B;middle]:\r\n        end = middle - 1\r\n    # target must be in upper half (middle-end)\r\n    else:\r\n        start = middle + 1\r\n \r\n# item has or has not been found \r\nif found:\r\n    print &quot;Found at index&quot; , middle\r\nelse:\r\n    print &quot;Not found&quot;\r\n<\/pre>\n<p>Python functions terminate as soon as a value is returned. This can remove the need for a &#8220;found&#8221; variable:<\/p>\n<pre class=\"brush: python; title: Python Function (unstructured); notranslate\" title=\"Python Function (unstructured)\">\r\ndef binary_search(array, target):\r\n    start = 0\r\n    end = len(array)\r\n\r\n    while start &lt;= end:\r\n        middle = (start + end) \/\/ 2\r\n        if target == array&#x5B;middle]:\r\n            return middle\r\n\r\n        if target &lt; array&#x5B;middle]:\r\n            end = middle - 1\r\n        else:\r\n            start = middle + 1\r\n\r\n    return -1\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Linear searching looks through a list\/array item by item until a target value is found, or the end of the list is reached. Linear searches can use unsorted lists, but are very inefficient. Binary searches only work on sorted lists, with the list repeatedly split into smaller and smaller lists, until the target value is found or the list is empty. Pointers are used to identify the range of the<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/adv-higher\/binary-search\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":291,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-633","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/comments?post=633"}],"version-history":[{"count":7,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/633\/revisions"}],"predecessor-version":[{"id":663,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/633\/revisions\/663"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/291"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/media?parent=633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}