{"id":11835,"date":"2023-01-19T05:27:36","date_gmt":"2023-01-19T05:27:36","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=11835"},"modified":"2023-09-22T08:01:44","modified_gmt":"2023-09-22T08:01:44","slug":"binary-search-in-data-structure","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/","title":{"rendered":"Binary Search In Data Structure"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543232-Binary%20Search%20In%20Data%20Structure.jpg\" alt=\"\" \/><\/p>\n<p>In this blog, we will talk about binary search in data structure. Binary Search is one of the most curious topics of data structures as it decreases the time complexity for searching an element in the array from O(N) to O(logN). Not only this, we will also discuss the advantages, drawbacks, applications, and when to use binary search in data structure. Let\u2019s walk slowly and discuss what is binary search in data structure and how to implement binary search in data structure in different languages.<\/p>\n<h2>Definition: Binary Search In C Language<\/h2>\n<p>Binary Search is a searching technique used in a sorted array by repeatedly dividing the search interval in half. Utilizing the knowledge that the array is sorted, the binary search focuses on decreasing the time complexity to O(LogN).<br \/>\nWith this method, an array&#8217;s middle is always searched for the element.<\/p>\n<p><strong>Note:<\/strong> A sorted list of items is the only type of list on which a binary search can be used. We must first sort the elements if they are not already sorted.<\/p>\n<h3>Dry Run For Binary Search In C Language<\/h3>\n<p>There are two techniques to implement binary search algorithms, which are explained below.<\/p>\n<ul>\n<li><strong>Iterative Method<\/strong><\/li>\n<li><strong>Recursive Method<\/strong><\/li>\n<\/ul>\n<p>The divide and conquer strategy is used in the recursive approach.<br \/>\nThe general procedures for both approaches are covered here.<\/p>\n<ol>\n<li>The array that will be used for searching is as follows:<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543343-Binary%20Search%20In%20Data%20Structure1.png\" alt=\"\" \/><\/li>\n<\/ol>\n<p>Let x = 4 serve as the search element.<\/p>\n<ol start=\"2\">\n<li>Place two pointers in the lowest and highest positions, respectively, at low and high.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543343-Binary%20Search%20In%20Data%20Structure2.png\" alt=\"\" \/><\/p>\n<ol start=\"3\">\n<li>Determine the array&#8217;s middle element, arr[(low + high)\/2], which equals 6.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543353-Binary%20Search%20In%20Data%20Structure3.png\" alt=\"\" \/><\/p>\n<ol start=\"4\">\n<li>\n<p>Return mid if x == mid. Otherwise, compare the searchable element with m.<\/p>\n<\/li>\n<li>\n<p>If x &gt; mid, evaluate x against the middle element of the elements to its right. Setting low to low = mid + 1 achieves this.<\/p>\n<\/li>\n<li>\n<p>Else, make a comparison of x with the element in the middle of the elements to the left of mid. Setting high to high = mid &#8211; 1 accomplishes this.<\/p>\n<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543353-Binary%20Search%20In%20Data%20Structure4.png\" alt=\"\" \/><\/p>\n<ol start=\"7\">\n<li>Carry on in this manner until low and high are met.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543353-Binary%20Search%20In%20Data%20Structure5.png\" alt=\"\" \/><\/p>\n<ol start=\"8\">\n<li>x = 4 is found.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543353-Binary%20Search%20In%20Data%20Structure6.png\" alt=\"\" \/><\/p>\n<h2>Pseudocode for Binary Search Algorithm<\/h2>\n<p>Here, we will see the pseudocode for binary search in data structure in both iterative as well as recursive methods.<\/p>\n<h3>Iteration Method<\/h3>\n<pre><code>do until the pointers low and high meet each other.\n    mid = (low + high)\/2\n    if (x == arr[mid])\n        return mid\n    else if (x &gt; arr[mid]) \/\/ x is on the right side\n        low = mid + 1\n    else                       \/\/ x is on the left side\n        high = mid - 1<\/code><\/pre>\n<h3>Recursive Method<\/h3>\n<pre><code>binarySearch(arr, x, low, high)\n    if low &gt; high\n        return False \n    else\n        mid = (low + high) \/ 2 \n        if x == arr[mid]\n            return mid\n        else if x &gt; arr[mid]        \/\/ x is on the right side\n            return binarySearch(arr, x, mid + 1, high)\n        else                               \/\/ x is on the right side\n            return binarySearch(arr, x, low, mid - 1)<\/code><\/pre>\n<p><strong>Code Implementation of Iterative Method For Binary Search In C language<\/strong><\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_11833 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_11833 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_11833 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_11833 .wpsm_nav-tabs > li.active > a, #tab_container_11833 .wpsm_nav-tabs > li.active > a:hover, #tab_container_11833 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_11833 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_11833 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_11833 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_11833 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_11833 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_11833 .wpsm_nav-tabs > li > a:hover , #tab_container_11833 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_11833 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_11833 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_11833 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_11833 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_11833 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_11833 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_11833 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11833 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_11833 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11833 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_11833 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_11833\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_11833\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11833_1\" aria-controls=\"tabs_desc_11833_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11833_2\" aria-controls=\"tabs_desc_11833_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11833_3\" aria-controls=\"tabs_desc_11833_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11833_4\" aria-controls=\"tabs_desc_11833_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_11833\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_11833_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">#include &lt;stdio.h&gt;\r\n\r\nint binarySearch(int array[], int x, int low, int high) {\r\n  while (low &lt;= high) {\r\n    int mid = low + (high - low) \/ 2;\r\n\r\n    if (array[mid] == x)\r\n      return mid;\r\n\r\n    if (array[mid] &lt; x)\r\n      low = mid + 1;\r\n\r\n    else\r\n      high = mid - 1;\r\n  }\r\n\r\n  return -1;\r\n}\r\n\r\nint main(void) {\r\n  int array[] = {3, 4, 5, 6, 7, 8, 9};\r\n  int n = sizeof(array) \/ sizeof(array[0]);\r\n  int x = 4;\r\n  int result = binarySearch(array, x, 0, n - 1);\r\n  if (result == -1)\r\n    printf(\"Not found\");\r\n  else\r\n    printf(\"Element is found at index %d\", result);\r\n  return 0;\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_11833_2\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nint binarySearch(int array[], int x, int low, int high) {\r\n  \r\n  while (low &lt;= high) {\r\n    int mid = low + (high - low) \/ 2;\r\n\r\n    if (array[mid] == x)\r\n      return mid;\r\n\r\n    if (array[mid] &lt; x)\r\n      low = mid + 1;\r\n\r\n    else\r\n      high = mid - 1;\r\n  }\r\n\r\n  return -1;\r\n}\r\n\r\nint main(void) {\r\n  int array[] = {3, 4, 5, 6, 7, 8, 9};\r\n  int x = 4;\r\n  int n = sizeof(array) \/ sizeof(array[0]);\r\n  int result = binarySearch(array, x, 0, n - 1);\r\n  if (result == -1)\r\n    printf(\"Not found\");\r\n  else\r\n    printf(\"Element is found at index %d\", result);\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_11833_3\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class BinarySearch {\r\n  int binarySearch(int array[], int x, int low, int high) {\r\n\r\n    while (low &lt;= high) {\r\n      int mid = low + (high - low) \/ 2;\r\n\r\n      if (array[mid] == x)\r\n        return mid;\r\n\r\n      if (array[mid] &lt; x)\r\n        low = mid + 1;\r\n\r\n      else\r\n        high = mid - 1;\r\n    }\r\n\r\n    return -1;\r\n  }\r\n\r\n  public static void main(String args[]) {\r\n    BinarySearch ob = new BinarySearch();\r\n    int array[] = { 3, 4, 5, 6, 7, 8, 9 };\r\n    int n = array.length;\r\n    int x = 4;\r\n    int result = ob.binarySearch(array, x, 0, n - 1);\r\n    if (result == -1)\r\n      System.out.println(\"Not found\");\r\n    else\r\n      System.out.println(\"Element found at index \" + result);\r\n  }\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_11833_4\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def binarySearch(array, x, low, high):\r\n\r\n    while low &lt;= high:\r\n        mid = low + (high - low)\/\/2\r\n        if array[mid] == x:\r\n            return mid\r\n        elif array[mid] &lt; x:\r\n            low = mid + 1\r\n        else:\r\n            high = mid - 1\r\n    return -1\r\n\r\n\r\narray = [3, 4, 5, 6, 7, 8, 9]\r\nx = 4\r\n\r\nresult = binarySearch(array, x, 0, len(array)-1)\r\n\r\nif result != -1:\r\n    print(\"Element is present at index \" + str(result))\r\nelse:\r\n    print(\"Not found\")<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_11833 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_11833 a\"),jQuery(\"#tab-content_11833\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Code Implementation of Recursive Method For Binary Search In C langauge<\/strong><\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_11834 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_11834 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_11834 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_11834 .wpsm_nav-tabs > li.active > a, #tab_container_11834 .wpsm_nav-tabs > li.active > a:hover, #tab_container_11834 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_11834 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_11834 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_11834 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_11834 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_11834 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_11834 .wpsm_nav-tabs > li > a:hover , #tab_container_11834 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_11834 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_11834 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_11834 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_11834 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_11834 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_11834 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_11834 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11834 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_11834 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11834 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_11834 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_11834\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_11834\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11834_1\" aria-controls=\"tabs_desc_11834_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-laptop\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11834_2\" aria-controls=\"tabs_desc_11834_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11834_3\" aria-controls=\"tabs_desc_11834_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11834_4\" aria-controls=\"tabs_desc_11834_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_11834\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_11834_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">#include &lt;stdio.h&gt;\r\n\r\nint binarySearch(int array[], int x, int low, int high) {\r\n  if (high &gt;= low) {\r\n    int mid = low + (high - low) \/ 2;\r\n\r\n    \/\/ If found at mid, then return it\r\n    if (array[mid] == x)\r\n      return mid;\r\n\r\n    \/\/ Search the left half\r\n    if (array[mid] &gt; x)\r\n      return binarySearch(array, x, low, mid - 1);\r\n\r\n    \/\/ Search the right half\r\n    return binarySearch(array, x, mid + 1, high);\r\n  }\r\n\r\n  return -1;\r\n}\r\n\r\nint main(void) {\r\n  int array[] = {3, 4, 5, 6, 7, 8, 9};\r\n  int n = sizeof(array) \/ sizeof(array[0]);\r\n  int x = 4;\r\n  int result = binarySearch(array, x, 0, n - 1);\r\n  if (result == -1)\r\n    printf(\"Not found\");\r\n  else\r\n    printf(\"Element is found at index %d\", result);\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_11834_2\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nint binarySearch(int array[], int x, int low, int high) {\r\n  if (high &gt;= low) {\r\n    int mid = low + (high - low) \/ 2;\r\n\r\n    \/\/ If found at mid, then return it\r\n    if (array[mid] == x)\r\n      return mid;\r\n\r\n    \/\/ Search the left half\r\n    if (array[mid] &gt; x)\r\n      return binarySearch(array, x, low, mid - 1);\r\n\r\n    \/\/ Search the right half\r\n    return binarySearch(array, x, mid + 1, high);\r\n  }\r\n\r\n  return -1;\r\n}\r\n\r\nint main(void) {\r\n  int array[] = {3, 4, 5, 6, 7, 8, 9};\r\n  int x = 4;\r\n  int n = sizeof(array) \/ sizeof(array[0]);\r\n  int result = binarySearch(array, x, 0, n - 1);\r\n  if (result == -1)\r\n    printf(\"Not found\");\r\n  else\r\n    printf(\"Element is found at index %d\", result);\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_11834_3\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class BinarySearch {\r\n  int binarySearch(int array[], int x, int low, int high) {\r\n\r\n    if (high &gt;= low) {\r\n      int mid = low + (high - low) \/ 2;\r\n\r\n      \/\/ If found at mid, then return it\r\n      if (array[mid] == x)\r\n        return mid;\r\n\r\n      \/\/ Search the left half\r\n      if (array[mid] &gt; x)\r\n        return binarySearch(array, x, low, mid - 1);\r\n\r\n      \/\/ Search the right half\r\n      return binarySearch(array, x, mid + 1, high);\r\n    }\r\n\r\n    return -1;\r\n  }\r\n\r\n  public static void main(String args[]) {\r\n    BinarySearch ob = new BinarySearch();\r\n    int array[] = { 3, 4, 5, 6, 7, 8, 9 };\r\n    int n = array.length;\r\n    int x = 4;\r\n    int result = ob.binarySearch(array, x, 0, n - 1);\r\n    if (result == -1)\r\n      System.out.println(\"Not found\");\r\n    else\r\n      System.out.println(\"Element found at index \" + result);\r\n  }\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_11834_4\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def binarySearch(array, x, low, high):\r\n\r\n    if high &gt;= low:\r\n\r\n        mid = low + (high - low)\/\/2\r\n\r\n        # If found at mid, then return it\r\n        if array[mid] == x:\r\n            return mid\r\n\r\n        # Search the left half\r\n        elif array[mid] &gt; x:\r\n            return binarySearch(array, x, low, mid-1)\r\n\r\n        # Search the right half\r\n        else:\r\n            return binarySearch(array, x, mid + 1, high)\r\n\r\n    else:\r\n        return -1\r\n\r\n\r\narray = [3, 4, 5, 6, 7, 8, 9]\r\nx = 4\r\n\r\nresult = binarySearch(array, x, 0, len(array)-1)\r\n\r\nif result != -1:\r\n    print(\"Element is present at index \" + str(result))\r\nelse:\r\n    print(\"Not found\")<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_11834 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_11834 a\"),jQuery(\"#tab-content_11834\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<h2>Binary Search in C language Complexity<\/h2>\n<p>Let\u2019s discuss the time and space complexity for binary search in data structure.<\/p>\n<h3>Time Complexities of Binary search in C language<\/h3>\n<ul>\n<li>Best case complexity: O(1)<\/li>\n<li>Average case complexity: O(log n)<\/li>\n<li>Worst case complexity: O(log n)<\/li>\n<\/ul>\n<h3>Space Complexity of Binary Search in C language<\/h3>\n<p>The space complexity of the binary search is O(1).<\/p>\n<h2>Advantages of Binary Search In C language:<\/h2>\n<ul>\n<li>Particularly for big arrays, binary search is quicker than linear search. The time it takes to complete a linear search grows linearly while the time it takes to complete a binary search grows logarithmically as the size of the array grows.<\/li>\n<li>In comparison to other search algorithms with a comparable time complexity, such interpolation search or exponential search, binary search is more effective.<\/li>\n<li>A binary search is a suitable option for many applications because it is practical and straightforward to understand.<\/li>\n<li>A binary search is a versatile approach since it can be applied to both sorted arrays and sorted linked lists.<\/li>\n<li>Large datasets kept in external memory, such as a hard drive or the cloud, are best searched via binary search.<\/li>\n<li>For more complex algorithms, such as those used in computer graphics and machine learning, binary search can be utilized as a building block.<\/li>\n<\/ul>\n<h2>Drawbacks of Binary Search In C Language:<\/h2>\n<ul>\n<li>The array must be sorted for us. Before running the search, we must first sort the array if it is not already sorted. The sorting step now has an additional O(N logN) time complexity, which can reduce the efficiency of binary search for very tiny arrays.<\/li>\n<li>The array being searched must be kept in a set of contiguous memory locations in order to use binary search. If the array is too big to fit in memory or is saved on external memory, such a hard drive or the cloud, this could be an issue.<\/li>\n<li>The array&#8217;s items must be similar, which means they must be able to be sorted, in order for binary search to work. If the array&#8217;s elements are not naturally arranged or the ordering is unclear, this could be an issue.<\/li>\n<li>When it comes to exploring really huge datasets that don&#8217;t fit in memory, binary search can be less effective than other techniques like hash tables.<\/li>\n<\/ul>\n<h2>Applications of Binary Search In C Language:<\/h2>\n<ul>\n<li>Binary search can be used as a building block for more complicated machine learning algorithms, such as those that train neural networks or identify the best hyperparameters for a model.<\/li>\n<li>Used frequently in competitive programming.<\/li>\n<li>Can be applied to computer graphics searches. The simpler binary search strategy can be used as a building element for more sophisticated computer graphics algorithms like ray tracing or texture mapping.<\/li>\n<li>Can be applied to database searches. A client database or a catalog of products are two examples of databases containing records that may be efficiently searched using binary search.<\/li>\n<\/ul>\n<h2>When to use Binary Search In C language:<\/h2>\n<ul>\n<li>With a time complexity of O(log n), it is substantially faster than linear search when searching a huge dataset.<\/li>\n<li>When the dataset has been sorted.<\/li>\n<li>When memory is contiguous and data is stored there.<\/li>\n<li>Data does not have complex relationships or structures.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will talk about binary search in data structure. Binary Search is one of the most curious topics of data structures as it decreases the time complexity for searching an element in the array from O(N) to O(logN). Not only this, we will also discuss the advantages, drawbacks, applications, and when to [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[136],"tags":[],"class_list":["post-11835","post","type-post","status-publish","format-standard","hentry","category-binary-search"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Binary Search In Data Structure<\/title>\n<meta name=\"description\" content=\"Here we will learn about binary search in data structure. We will also learn dry run and different methods used for binary search.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Binary Search In Data Structure\" \/>\n<meta property=\"og:description\" content=\"Here we will learn about binary search in data structure. We will also learn dry run and different methods used for binary search.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-19T05:27:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-22T08:01:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543232-Binary%20Search%20In%20Data%20Structure.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Binary Search In Data Structure\",\"datePublished\":\"2023-01-19T05:27:36+00:00\",\"dateModified\":\"2023-09-22T08:01:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/\"},\"wordCount\":940,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543232-Binary%20Search%20In%20Data%20Structure.jpg\",\"articleSection\":[\"binary search\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/\",\"name\":\"Binary Search In Data Structure\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543232-Binary%20Search%20In%20Data%20Structure.jpg\",\"datePublished\":\"2023-01-19T05:27:36+00:00\",\"dateModified\":\"2023-09-22T08:01:44+00:00\",\"description\":\"Here we will learn about binary search in data structure. We will also learn dry run and different methods used for binary search.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543232-Binary%20Search%20In%20Data%20Structure.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543232-Binary%20Search%20In%20Data%20Structure.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"binary search\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/binary-search\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Binary Search In Data Structure\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Binary Search In Data Structure","description":"Here we will learn about binary search in data structure. We will also learn dry run and different methods used for binary search.","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:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/","og_locale":"en_US","og_type":"article","og_title":"Binary Search In Data Structure","og_description":"Here we will learn about binary search in data structure. We will also learn dry run and different methods used for binary search.","og_url":"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-01-19T05:27:36+00:00","article_modified_time":"2023-09-22T08:01:44+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543232-Binary%20Search%20In%20Data%20Structure.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Binary Search In Data Structure","datePublished":"2023-01-19T05:27:36+00:00","dateModified":"2023-09-22T08:01:44+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/"},"wordCount":940,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543232-Binary%20Search%20In%20Data%20Structure.jpg","articleSection":["binary search"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/","url":"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/","name":"Binary Search In Data Structure","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543232-Binary%20Search%20In%20Data%20Structure.jpg","datePublished":"2023-01-19T05:27:36+00:00","dateModified":"2023-09-22T08:01:44+00:00","description":"Here we will learn about binary search in data structure. We will also learn dry run and different methods used for binary search.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543232-Binary%20Search%20In%20Data%20Structure.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1674102543232-Binary%20Search%20In%20Data%20Structure.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/binary-search-in-data-structure\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"binary search","item":"https:\/\/prepbytes.com\/blog\/category\/binary-search\/"},{"@type":"ListItem","position":3,"name":"Binary Search In Data Structure"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=11835"}],"version-history":[{"count":3,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11835\/revisions"}],"predecessor-version":[{"id":12591,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11835\/revisions\/12591"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=11835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=11835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=11835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}