{"id":1429,"date":"2022-03-03T19:44:04","date_gmt":"2022-03-03T19:44:04","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1429"},"modified":"2022-03-03T19:44:04","modified_gmt":"2022-03-03T19:44:04","slug":"program-to-perform-binary-search","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/program-to-perform-binary-search\/","title":{"rendered":"Program to perform binary search"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Example Program to perform binary search on a list of integer numbers<\/h4>\n\n\n\n<p>This program uses\u00a0binary search algorithm\u00a0to search an element in given list of elements.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Program: Binary Search Example\n * Written by: Chaitanya from beginnersbook.com\n * Input: Number of elements, element's values, value to be searched\n * Output:Position of the number input by user among other numbers*\/\nimport java.util.Scanner;\nclass BinarySearchExample\n{\n   public static void main(String args&#91;])\n   {\n      int counter, num, item, array&#91;], first, last, middle;\n      \/\/To capture user input\n      Scanner input = new Scanner(System.in);\n      System.out.println(\"Enter number of elements:\");\n      num = input.nextInt(); \n\n      \/\/Creating array to store the all the numbers\n      array = new int&#91;num];\n\n      System.out.println(\"Enter \" + num + \" integers\");\n      \/\/Loop to store each numbers in array\n      for (counter = 0; counter &lt; num; counter++)\n          array&#91;counter] = input.nextInt();\n\n      System.out.println(\"Enter the search value:\");\n      item = input.nextInt();\n      first = 0;\n      last = num - 1;\n      middle = (first + last)\/2;\n\n      while( first &lt;= last )\n      {\n         if ( array&#91;middle] &lt; item )\n           first = middle + 1;\n         else if ( array&#91;middle] == item )\n         {\n           System.out.println(item + \" found at location \" + (middle + 1) + \".\");\n           break;\n         }\n         else\n         {\n             last = middle - 1;\n         }\n         middle = (first + last)\/2;\n      }\n      if ( first &gt; last )\n          System.out.println(item + \" is not found.\\n\");\n   }\n}<\/code><\/pre>\n\n\n\n<p>Output 1:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter number of elements:\n7\nEnter 7 integers\n4\n5\n66\n77\n8\n99\n0\nEnter the search value:\n77\n77 found at location 4.<\/code><\/pre>\n\n\n\n<p>Output 2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter number of elements:\n5\nEnter 5 integers\n12\n3\n77\n890\n23\nEnter the search value:\n99\n99 is not found.<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Example Program to perform binary search on a list of integer numbers This program uses\u00a0binary search algorithm\u00a0to search an element in given list of elements. Output 1: Output 2:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[586],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1429"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1429"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1429\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}