{"id":151,"date":"2023-06-11T20:14:11","date_gmt":"2023-06-11T20:14:11","guid":{"rendered":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/?page_id=151"},"modified":"2023-11-04T13:42:49","modified_gmt":"2023-11-04T13:42:49","slug":"binary-search","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/","title":{"rendered":"Binary Search"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Binary Search<\/h2>\n<div class=\"tabcontent\">\n\n<h3 class=\"\">What is Binary Search?<\/h3>\n<p>Binary search is an efficient algorithm used to find a specific element in a sorted array or list. It works by repeatedly dividing the search space in half until the target element is found or determined to be absent.<\/p>\n<h3 class=\"\">How does Binary Search work?<\/h3>\n<ol>\n<li>Binary search starts by comparing the target element with the middle element of the array.\n<ul>\n<li>If they are equal, the search is complete.<\/li>\n<li>If the target element is smaller, the search continues on the lower half of the array;<\/li>\n<li>otherwise, it continues on the upper half.<\/li>\n<\/ul>\n<\/li>\n<li>This process is repeated until the target element is found or the search space is empty.<\/li>\n<\/ol>\n\n<\/div><h2 class=\"tabtitle\">Demonstration<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Binary Search Demonstration<\/h3>\n<p>Have a go with the binary search demonstration below.<\/p>\n<p><iframe loading=\"lazy\" title=\"Binary Search\" src=\"https:\/\/revise.learnlearn.uk\/teachassist\/static\/static_pages\/binary_search.html\" width=\"800\" height=\"430\" frameborder=\"0\"><\/iframe><\/p>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Pros &amp; Cons<\/h2>\n<div class=\"tabcontent\">\n\n<h2 class=\"slide-title\">Pros and Cons of Binary Search<\/h2>\n<div class=\"slide-content\">\n<h3 class=\"\">Pros<\/h3>\n<ul>\n<li>Efficient searching algorithm<\/li>\n<li>Divides the search space in half with each comparison<\/li>\n<li>Requires a sorted array, ensuring data integrity<\/li>\n<li>Can quickly find a desired element in a large data set<\/li>\n<\/ul>\n<h3 class=\"\">Cons<\/h3>\n<ul>\n<li>Requires a sorted array, making initial setup time-consuming<\/li>\n<li>Not suitable for dynamic data sets that frequently change<\/li>\n<li>Cannot handle unsorted or unordered data<\/li>\n<li>Implementation is more complex than a linear search<\/li>\n<\/ul>\n<\/div>\n\n<\/div><h2 class=\"tabtitle\">Python Code<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Binary Search Python Code<\/h3>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">binary_search<\/span>(arr, target):\r\n    low <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>\r\n    high <span style=\"color: #333333;\">=<\/span> <span style=\"color: #007020;\">len<\/span>(arr) <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">while<\/span> low <span style=\"color: #333333;\">&lt;=<\/span> high:\r\n        mid <span style=\"color: #333333;\">=<\/span> (low <span style=\"color: #333333;\">+<\/span> high) <span style=\"color: #333333;\">\/\/<\/span> <span style=\"color: #0000dd; font-weight: bold;\">2<\/span>\r\n\r\n        <span style=\"color: #008800; font-weight: bold;\">if<\/span> arr[mid] <span style=\"color: #333333;\">==<\/span> target:\r\n            <span style=\"color: #008800; font-weight: bold;\">return<\/span> mid\r\n        <span style=\"color: #008800; font-weight: bold;\">elif<\/span> arr[mid] <span style=\"color: #333333;\">&lt;<\/span> target:\r\n            low <span style=\"color: #333333;\">=<\/span> mid <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">else<\/span>:\r\n            high <span style=\"color: #333333;\">=<\/span> mid <span style=\"color: #333333;\">-<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>\r\n\r\n<span style=\"color: #888888;\"># Example usage<\/span>\r\narray <span style=\"color: #333333;\">=<\/span> [<span style=\"color: #0000dd; font-weight: bold;\">2<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">5<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">8<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">12<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">16<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">23<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">38<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">56<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">72<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">91<\/span>]\r\ntarget <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">23<\/span>\r\n\r\nresult <span style=\"color: #333333;\">=<\/span> binary_search(array, target)\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">if<\/span> result <span style=\"color: #333333;\">!=<\/span> <span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>:\r\n    <span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Element found at index\"<\/span>, result)\r\n<span style=\"color: #008800; font-weight: bold;\">else<\/span>:\r\n    <span style=\"color: #008800; font-weight: bold;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Element not found\"<\/span>)\r\n<\/pre>\n<\/div>\n<\/div><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Binary Search? Binary search is an efficient algorithm used to find a specific element in a sorted array or list. It works by repeatedly dividing the search space in half until the target element is found or determined to be absent. How does Binary Search work? Binary search starts by comparing the target&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Binary Search<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"off","neve_meta_content_width":100,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Binary Search - Edexcel iGCSE Computer Science<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Binary Search - Edexcel iGCSE Computer Science\" \/>\n<meta property=\"og:description\" content=\"What is Binary Search? Binary search is an efficient algorithm used to find a specific element in a sorted array or list. It works by repeatedly dividing the search space in half until the target element is found or determined to be absent. How does Binary Search work? Binary search starts by comparing the target&hellip;&nbsp;Read More &raquo;Binary Search\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/\" \/>\n<meta property=\"og:site_name\" content=\"Edexcel iGCSE Computer Science\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-04T13:42:49+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/\",\"url\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/\",\"name\":\"Binary Search - Edexcel iGCSE Computer Science\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/#website\"},\"datePublished\":\"2023-06-11T20:14:11+00:00\",\"dateModified\":\"2023-11-04T13:42:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Binary Search\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/#website\",\"url\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/\",\"name\":\"Edexcel iGCSE Computer Science\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Binary Search - Edexcel iGCSE Computer Science","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/","og_locale":"en_GB","og_type":"article","og_title":"Binary Search - Edexcel iGCSE Computer Science","og_description":"What is Binary Search? Binary search is an efficient algorithm used to find a specific element in a sorted array or list. It works by repeatedly dividing the search space in half until the target element is found or determined to be absent. How does Binary Search work? Binary search starts by comparing the target&hellip;&nbsp;Read More &raquo;Binary Search","og_url":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/","og_site_name":"Edexcel iGCSE Computer Science","article_modified_time":"2023-11-04T13:42:49+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/","url":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/","name":"Binary Search - Edexcel iGCSE Computer Science","isPartOf":{"@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/#website"},"datePublished":"2023-06-11T20:14:11+00:00","dateModified":"2023-11-04T13:42:49+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/binary-search\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/"},{"@type":"ListItem","position":2,"name":"Binary Search"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/#website","url":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/","name":"Edexcel iGCSE Computer Science","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"What is Binary Search? Binary search is an efficient algorithm used to find a specific element in a sorted array or list. It works by repeatedly dividing the search space in half until the target element is found or determined to be absent. How does Binary Search work? Binary search starts by comparing the target&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/pages\/151"}],"collection":[{"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":7,"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/pages\/151\/revisions"}],"predecessor-version":[{"id":607,"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/pages\/151\/revisions\/607"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/media?parent=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}