{"id":11072,"date":"2022-12-15T09:50:33","date_gmt":"2022-12-15T09:50:33","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=11072"},"modified":"2023-04-18T13:33:28","modified_gmt":"2023-04-18T13:33:28","slug":"binary-search-program-in-java","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/","title":{"rendered":"Binary Search Program in Java"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671097309628-binary%20search%20program%20in%20java.jpg\" alt=\"\" \/><\/p>\n<p>Searching is one of the most fundamental operations in computer science. It involves finding a specific element from a given data set. There are several search algorithms available, and one of the most efficient and widely used algorithms is the Binary search algorithm. In this article, we will discuss Binary Search in Java, its logic, and its implementation.<\/p>\n<h2>The Logic of Binary Search in Java<\/h2>\n<p>The logic of binary search in Java is to search for an element in a sorted array by repeatedly dividing the search interval in half. The algorithm starts by comparing the element to be searched with the middle element of the array. If the element matches the middle element, the search is successful, and the index of the middle element is returned.<\/p>\n<p>If the element is less than the middle element, the search is continued in the left half of the array, as the element can only be present in that half. If the element is greater than the middle element, the search is continued in the right half of the array, as the element can only be present in that half.<\/p>\n<p>The search interval is again divided in half, and the process continues until the element is found or until the search interval becomes empty.<\/p>\n<p>The key idea behind binary search is to reduce the search space by half at every step of the algorithm. This reduces the number of comparisons required to find the element, making the search more efficient. The time complexity of the binary search is O(log n), where n is the number of elements in the array.<\/p>\n<h2>Binary Search in Java Explanation<\/h2>\n<p>Item to be searched=20<br \/>\ninput:<\/p>\n<table>\n<thead>\n<tr>\n<th>0<\/th>\n<th>1<\/th>\n<th>2<\/th>\n<th>3<\/th>\n<th>4<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>10<\/td>\n<td>11<\/td>\n<td>16<\/td>\n<td>20<\/td>\n<td>23<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>beg=0, end=4, mid=2<\/p>\n<table>\n<thead>\n<tr>\n<th>0<\/th>\n<th>1<\/th>\n<th>2<\/th>\n<th>3<\/th>\n<th>4<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>10<\/td>\n<td>11<\/td>\n<td>16<\/td>\n<td>20<\/td>\n<td>23<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>beg=3, end=4, mid=3<\/p>\n<table>\n<thead>\n<tr>\n<th>0<\/th>\n<th>1<\/th>\n<th>2<\/th>\n<th>3<\/th>\n<th>4<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>10<\/td>\n<td>11<\/td>\n<td>16<\/td>\n<td>20<\/td>\n<td>23<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Element found at index 3, Hence 3 will get returned.<\/p>\n<h2>Algorithm of Binary search in Java<\/h2>\n<p>The general steps for both methods (iterative and recursive) are discussed below:<\/p>\n<ul>\n<li><strong>Step 1<\/strong> &#8211; Read the user\u2019s search element.<\/li>\n<li><strong>Step 2<\/strong> &#8211; Initialize the lower and upper bounds of the search interval. For the iterative method, this is done using two variables l and r, whereas, for the recursive method, this is done by passing the bounds as arguments to the function.<\/li>\n<li><strong>Step 3<\/strong> &#8211; Find the middle element of the search interval by calculating the average of the lower and upper bounds. For the iterative method, this is done using the formula m = l + (r &#8211; l) \/ 2, whereas for the recursive method, this is done inside the function by calculating mid = l + (r &#8211; l) \/ 2.<\/li>\n<li><strong>Step 4<\/strong> &#8211; Compare the middle element with the element to be searched. If they are equal, return the index of the middle element.<\/li>\n<li><strong>Step 5<\/strong> &#8211; If the element is less than the middle element, then the search continues in the left half of the array. For the iterative method, this is done by updating the upper bound to r = m &#8211; 1, whereas, for the recursive method, this is done by making a recursive call to the function with the same lower bound and a new upper bound of m &#8211; 1.<\/li>\n<li><strong>Step 6<\/strong> &#8211; If the element is greater than the middle element, then the search continues in the right half of the array. For the iterative method, this is done by updating the lower bound to l = m + 1, whereas, for the recursive method, this is done by making a recursive call to the function with the same upper bound and a new lower bound of m + 1.<\/li>\n<li><strong>Step 7<\/strong> &#8211; Repeat steps 3-6 until the element is found or the search interval is empty.<\/li>\n<li><strong>Step 8<\/strong> &#8211; If the element is not found in the array, return -1 and display &quot;Element is not found in the array!!!&quot;.<\/li>\n<\/ul>\n<h2>Pseudo Code for Both the Logics (Recursive and Iterative)<\/h2>\n<p><strong>Iteration Method of Binary Search in Java<\/strong><br \/>\ndo until the pointers low and high meet each other.<\/p>\n<pre><code>    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<p><strong>Recursive Method of Binary Search in Java<\/strong><br \/>\nbinarySearch(arr, x, low, high)<\/p>\n<pre><code>    if high &gt;= low\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)\n    return -1<\/code><\/pre>\n<h2>Program for Binary Search in Java Using Recursive Approach<\/h2>\n<p>Below is the code implementation and explanation of the program for Binary Search in Java using the recursive approach:<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_11049 {\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_11049 .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_11049 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_11049 .wpsm_nav-tabs > li.active > a, #tab_container_11049 .wpsm_nav-tabs > li.active > a:hover, #tab_container_11049 .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_11049 .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_11049 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_11049 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_11049 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_11049 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_11049 .wpsm_nav-tabs > li > a:hover , #tab_container_11049 .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_11049 .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_11049 .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_11049 .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_11049 .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_11049 .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_11049 .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_11049 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11049 .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_11049 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11049 .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_11049 .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_11049\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_11049\">\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_11049_1\" aria-controls=\"tabs_desc_11049_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>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\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_11049\">\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_11049_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nimport java.util.*;\r\nclass PrepBytes {\r\n\tint binarySearch(int arr[], int l, int r, int x)\r\n\t{\r\n\t\tif (r &gt;= l &amp;&amp; l &lt;= arr.length - 1) {\r\n\t\t\tint mid = l + (r - l) \/ 2;\r\n\t\t\tif (arr[mid] == x)\r\n\t\t\t\treturn mid;\t\t\t\t\r\n\t\t\tif (arr[mid] &gt; x)\r\n\t\t\t\treturn binarySearch(arr, l, mid - 1, x);\r\n\t\t\treturn binarySearch(arr, mid + 1, r, x);\r\n\t\t}\r\n\t\treturn -1;\r\n\t}\r\n\tpublic static void main(String args[])\r\n\t{\r\n\t\tPrepBytes ob = new PrepBytes();\r\n\t\tint arr[] = { 2, 3, 4, 10, 40 };\r\n\t\tint n = arr.length;\r\n\t\tint x = 10;\r\n\t\tint result = ob.binarySearch(arr, 0, n - 1, x);\t\t\r\n\t\tif (result == -1)\r\n\t\t\tSystem.out.println(&quot;Element not present&quot;);\r\n\t\telse\r\n\t\t\tSystem.out.println(&quot;Element found at index &quot; + result);\r\n\t}\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_11049 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_11049 a\"),jQuery(\"#tab-content_11049\"));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>Output<\/strong><\/p>\n<pre><code>Element found at index 3<\/code><\/pre>\n<p><strong>Explanation:<\/strong> The above code is the implementation of Binary Search in Java. The class &quot;PrepBytes&quot; contains a method &quot;binarySearch&quot;. The binary search method first checks if the starting index &quot;l&quot; is less than or equal to the ending index &quot;r&quot; and if the starting index &quot;l&quot; is less than or equal to the length of the array &quot;arr&quot;. If not, it returns -1 indicating that the element is not present in the array.<\/p>\n<p>If the above condition is true, it calculates the mid index using the formula (l + r) \/ 2. It then checks if the element at the mid index is equal to the element to be searched &quot;x&quot;. If it is, it returns the mid-index.<\/p>\n<p>If the element at the mid index is greater than the element to be searched &quot;x&quot;, it means that the element can only be present in the left half of the array. Therefore, the method calls itself recursively with the left half of the array.<\/p>\n<p>If the element at the mid index is smaller than the element to be searched &quot;x&quot;, it means that the element can only be present in the right half of the array. Therefore, the method calls itself recursively with the right half of the array.<\/p>\n<h2>Program for Binary Search in Java Using Iterative Approach<\/h2>\n<p>Below is the code implementation and explanation of the program for Binary Search in Java using the iterative approach:<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_11050 {\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_11050 .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_11050 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_11050 .wpsm_nav-tabs > li.active > a, #tab_container_11050 .wpsm_nav-tabs > li.active > a:hover, #tab_container_11050 .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_11050 .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_11050 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_11050 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_11050 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_11050 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_11050 .wpsm_nav-tabs > li > a:hover , #tab_container_11050 .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_11050 .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_11050 .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_11050 .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_11050 .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_11050 .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_11050 .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_11050 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11050 .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_11050 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11050 .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_11050 .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_11050\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_11050\">\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_11050_1\" aria-controls=\"tabs_desc_11050_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>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\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_11050\">\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_11050_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass PrepBytes {\r\n\r\n\tint binarySearch(int arr[], int x)\r\n\t{\r\n\r\n\t\tint l = 0, r = arr.length - 1;\r\n\r\n\t\twhile (l &lt;= r) {\r\n\t\t\tint m = l + (r - l) \/ 2;\r\n\t\t\tif (arr[m] == x)\r\n\t\t\t\treturn m;\r\n\r\n\t\t\tif (arr[m] &lt; x)\r\n\t\t\t\tl = m + 1;\r\n\t\t\telse\r\n\t\t\t\tr = m - 1;\r\n\t\t}\r\n\r\n\t\treturn -1;\r\n\t}\r\n\r\n\tpublic static void main(String args[])\r\n\t{\r\n\r\n\t\tPrepBytes ob = new PrepBytes();\r\n\r\n\r\n\t\tint arr[] = { 2, 3, 4, 10, 40 };\r\n\t\tint n = arr.length;\r\n\t\tint x = 10;\r\n\t\tint result = ob.binarySearch(arr, x);\r\n\t\tif (result == -1)\r\n\t\t\tSystem.out.println(&quot;Element not present&quot;);\r\n\t\telse\r\n\t\t\tSystem.out.println(&quot;Element found at index &quot; + result);\r\n\t}\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\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_11050 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_11050 a\"),jQuery(\"#tab-content_11050\"));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>Output<\/strong><\/p>\n<pre><code>Element is found at index 3<\/code><\/pre>\n<p><strong>Explanation:<\/strong> The above code is an implementation of Binary Search in Java. The class &quot;PrepBytes&quot; contains a method &quot;binarySearch&quot;. The binary search method first initializes two variables &quot;l&quot; and &quot;r&quot; to 0 and &quot;arr.length &#8211; 1&quot; respectively. It then enters into a while loop that executes until the value of &quot;l&quot; is less than or equal to the value of &quot;r&quot;.<\/p>\n<p>Inside the loop, the method calculates the mid index &quot;m&quot; using the formula (l + r) \/ 2. It then checks if the element at the mid index is equal to the element to be searched &quot;x&quot;. If it is, it returns the mid-index.<\/p>\n<p>If the element at the mid index is smaller than the element to be searched &quot;x&quot;, it means that the element can only be present in the right half of the array. Therefore, the method updates the value of &quot;l&quot; to &quot;m + 1&quot;.<\/p>\n<p>If the element at the mid index is greater than the element to be searched &quot;x&quot;, it means that the element can only be present in the left half of the array. Therefore, the method updates the value of &quot;r&quot; to &quot;m &#8211; 1&quot;.<\/p>\n<p>If the element is not found in the array, the while loop will terminate, and the method will return -1 indicating that the element is not present in the array.<\/p>\n<h2>Time Complexity for Binary Search in Java<\/h2>\n<table>\n<thead>\n<tr>\n<th>Scenario<\/th>\n<th>Time Complexity<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Best Case<\/td>\n<td>O(1)<\/td>\n<\/tr>\n<tr>\n<td>Average Case<\/td>\n<td>O(logn)<\/td>\n<\/tr>\n<tr>\n<td>Worst Case<\/td>\n<td>O(logn)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Best Case Complexity<\/strong> &#8211; In a binary search, the best case scenario is when the searchable element is discovered in the first comparison, or when the first middle element is in fact the searchable element. Binary search has a best-case temporal complexity of O(1).<\/p>\n<p><strong>Average Case Complexity<\/strong> &#8211; For a binary search, the average case time complexity is O(logn).<\/p>\n<p><strong>Worst Case Complexity<\/strong> &#8211; The worst case scenario for binary search is when we have to continuously narrow the search space until there is just one element. Binary search has a worst-case temporal complexity of O(logn).<\/p>\n<h2>Space Complexity for Binary Search in Java<\/h2>\n<p>The Space complexity for coding binary search program in Java is O(1).<\/p>\n<p><strong>Tips:<\/strong> On sorted array elements, binary search can be applied. We must first sort the list elements if they are not already organized in a sorted fashion.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nIn conclusion, Binary Search is a highly efficient algorithm for searching a specific element in a sorted array. With its O(log n) time complexity, it is widely used in various computer science applications. Both recursive and iterative approaches can be used to implement Binary Search in Java, making it a versatile tool for developers.<\/p>\n<h2>FAQs<\/h2>\n<p>Here are some frequently asked questions on Binary Search in Java.<\/p>\n<p><strong>Q1: Can Binary Search be used for an unsorted array in Java?<\/strong><br \/>\n<strong>Ans:<\/strong> No, Binary Search cannot be used for an unsorted array in Java. It only works with a sorted array.<\/p>\n<p><strong>Q2: What happens when the element to be searched is not present in the array in Java?<\/strong><br \/>\n<strong>Ans:<\/strong> If the element to be searched is not present in the array, the Binary Search algorithm returns -1.<\/p>\n<p><strong>Q3: Is Binary Search faster than Linear Search in Java?<\/strong><br \/>\n<strong>Ans:<\/strong> Yes, Binary Search is faster than Linear Search in Java for large datasets because of its O(log n) time complexity.<\/p>\n<p><strong>Q4: Can Binary Search be performed on a linked list in Java?<\/strong><br \/>\n<strong>Ans:<\/strong> No, Binary Search cannot be performed on a linked list in Java because linked lists do not provide random access.<\/p>\n<p><strong>Q5: What is the advantage of using the recursive approach in Binary Search in Java?<\/strong><br \/>\n<strong>Ans:<\/strong> The advantage of using the recursive approach in Binary Search in Java is that it is easy to understand and implement.<\/p>\n<p><strong>Q6: What is the advantage of using the iterative approach in Binary Search in Java?<\/strong><br \/>\n<strong>Ans:<\/strong> The advantage of using the iterative approach in Binary Search in Java is that it is more efficient and faster than the recursive approach.<\/p>\n<p><strong>Q7: Can Binary Search be used for multidimensional arrays in Java?<\/strong><br \/>\n<strong>Ans:<\/strong> No, Binary Search cannot be used for multidimensional arrays in Java because it only works for one-dimensional arrays.<\/p>\n<p><strong>Other Java Programs<\/strong><\/p>\n<p><a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-add-two-numbers\/\" title=\"Java Program to Add Two Numbers\">Java Program to Add Two Numbers<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-check-prime-number\/\" title=\"Java Program to Check Prime Number\">Java Program to Check Prime Number<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-check-whether-a-number-is-a-palindrome-or-not\/\" title=\"Java Program to Check Whether a Number is a Palindrome or Not\">Java Program to Check Whether a Number is a Palindrome or Not<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-find-the-factorial-of-a-number\/\" title=\"Java Program to Find the Factorial of a Number\">Java Program to Find the Factorial of a Number<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-reverse-a-number\/\" title=\"Java Program to Reverse a Number\">Java Program to Reverse a Number<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-search-an-element-in-a-linked-list\/\" title=\"Java Program to search an element in a Linked List\">Java Program to search an element in a Linked List<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/program-to-convert-arraylist-to-linkedlist-in-java\/\" title=\"Program to convert ArrayList to LinkedList in Java\">Program to convert ArrayList to LinkedList in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/java-program-to-reverse-a-linked-list\/\" title=\"Java Program to Reverse a linked list\">Java Program to Reverse a linked list<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/java-program-to-search-an-element-in-a-linked-list\/\" title=\"Java Program to search an element in a Linked List\">Java Program to search an element in a Linked List<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/anagram-program-in-java\/\" title=\"Anagram Program in Java\">Anagram Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/inheritance-program-in-java\/\" title=\"Inheritance Program in Java\">Inheritance Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/even-odd-program-in-java\/\" title=\"Even Odd Program in Java\">Even Odd Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/hello-world-program-in-java\/\" title=\"Hello World Program in Java\">Hello World Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/if-else-program-in-java\/\" title=\"If else Program in Java\">If else Program in Java<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Searching is one of the most fundamental operations in computer science. It involves finding a specific element from a given data set. There are several search algorithms available, and one of the most efficient and widely used algorithms is the Binary search algorithm. In this article, we will discuss Binary Search in Java, its logic, [&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":[143],"tags":[],"class_list":["post-11072","post","type-post","status-publish","format-standard","hentry","category-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Binary Search Program in Java<\/title>\n<meta name=\"description\" content=\"Discuss how binary search works, its algorithm and many binary search program approaches with different method in Java.\" \/>\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-program-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Binary Search Program in Java\" \/>\n<meta property=\"og:description\" content=\"Discuss how binary search works, its algorithm and many binary search program approaches with different method in Java.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/\" \/>\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=\"2022-12-15T09:50:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-18T13:33:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671097309628-binary%20search%20program%20in%20java.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=\"6 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-program-in-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Binary Search Program in Java\",\"datePublished\":\"2022-12-15T09:50:33+00:00\",\"dateModified\":\"2023-04-18T13:33:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/\"},\"wordCount\":1756,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671097309628-binary%20search%20program%20in%20java.jpg\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/\",\"name\":\"Binary Search Program in Java\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671097309628-binary%20search%20program%20in%20java.jpg\",\"datePublished\":\"2022-12-15T09:50:33+00:00\",\"dateModified\":\"2023-04-18T13:33:28+00:00\",\"description\":\"Discuss how binary search works, its algorithm and many binary search program approaches with different method in Java.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671097309628-binary%20search%20program%20in%20java.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671097309628-binary%20search%20program%20in%20java.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/java\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Binary Search Program in Java\"}]},{\"@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 Program in Java","description":"Discuss how binary search works, its algorithm and many binary search program approaches with different method in Java.","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-program-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Binary Search Program in Java","og_description":"Discuss how binary search works, its algorithm and many binary search program approaches with different method in Java.","og_url":"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2022-12-15T09:50:33+00:00","article_modified_time":"2023-04-18T13:33:28+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671097309628-binary%20search%20program%20in%20java.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Binary Search Program in Java","datePublished":"2022-12-15T09:50:33+00:00","dateModified":"2023-04-18T13:33:28+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/"},"wordCount":1756,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671097309628-binary%20search%20program%20in%20java.jpg","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/","url":"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/","name":"Binary Search Program in Java","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671097309628-binary%20search%20program%20in%20java.jpg","datePublished":"2022-12-15T09:50:33+00:00","dateModified":"2023-04-18T13:33:28+00:00","description":"Discuss how binary search works, its algorithm and many binary search program approaches with different method in Java.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671097309628-binary%20search%20program%20in%20java.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1671097309628-binary%20search%20program%20in%20java.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/binary-search-program-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/prepbytes.com\/blog\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Binary Search Program in Java"}]},{"@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\/11072","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=11072"}],"version-history":[{"count":3,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11072\/revisions"}],"predecessor-version":[{"id":15745,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11072\/revisions\/15745"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=11072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=11072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=11072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}