{"id":19277,"date":"2020-07-10T14:33:41","date_gmt":"2020-07-10T21:33:41","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=19277"},"modified":"2023-12-01T03:53:33","modified_gmt":"2023-12-01T11:53:33","slug":"binary-search-java","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/binary-search-java\/","title":{"rendered":"Binary Search Java: A Guide"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-how-to-write-a-binary-search-algorithm-in-java\">How to Write a Binary Search Algorithm in Java<\/h2>\n\n\n\n<p>Computers do not search through items like humans do. Whereas humans can change their approach to finding something, computers need to be given specific instructions on how to locate a particular item. That\u2019s where standard algorithms are useful.<br><\/p>\n\n\n\n<p>Binary searches are an example of a standard algorithm. They are used to find an element in a sorted array of items. In this guide, we\u2019re going to talk about what binary searches are, how they work, and how you can implement them in Java. We\u2019ll walk through two examples of a binary search: one using the recursive approach, and the other using the iterative approach.<br><\/p>\n\n\n\n<p>Let\u2019s get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-a-binary-search\">What is a Binary Search?<\/h2>\n\n\n\n<p>A binary search is a search algorithm that locates the position of an element in a sorted <a href=\"https:\/\/careerkarma.com\/blog\/java-array\/\">array<\/a>.<br><\/p>\n\n\n\n<p>Binary searches start by dividing a list in half. The search will then compare the middle number to the number for which the algorithm is searching.<br><\/p>\n\n\n\n<p>If the number is smaller than the middle number, this process is repeated for the lower half of the list; otherwise, this process is repeated for the upper half of the list.<br><\/p>\n\n\n\n<p>Binary searches are more efficient than linear searches, another common type of search. This is because each time the algorithm looks for an item, it reduces the number of items to search through by a factor of two.<br><\/p>\n\n\n\n<p>Binary searches can only be executed on a sorted list of items. This means that if you don\u2019t already have a sorted list, you will need to sort it using a sorting algorithm before running a binary search.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-do-binary-searches-work\">How do Binary Searches Work?<\/h2>\n\n\n\n<p>Binary searches can be implemented in two ways: iteratively or recursively.<br><\/p>\n\n\n\n<p>An iterative binary search is one where loops are used to search for an item in a list. A recursive binary search uses a function that calls itself again and again to find an item in a list. Recursive binary searches use the divide and conquer approach to find an item.<br><\/p>\n\n\n\n<p>You can learn more about recursion in our <a href=\"https:\/\/careerkarma.com\/blog\/java-recursion\/\">guide to Java recursion<\/a>.<br><\/p>\n\n\n\n<p>The general algorithm for implementing a <a href=\"https:\/\/careerkarma.com\/blog\/learn-to-code-in-binary\/\">binary search<\/a> is the same no matter which approach you decide to use.<br><\/p>\n\n\n\n<p>Consider the following list:<br><\/p>\n\n\n\n<figure class=\"wp-block-table course-info-table\"><table><tbody><tr><td>6<\/td><td>7<\/td><td>8<\/td><td>9<\/td><td>10<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>We\u2019re going to search for the number 7 in our list. To start, we are going to set two pointers which represent the lowest and highest positions in our list:<br><\/p>\n\n\n\n<figure class=\"wp-block-table course-info-table\"><table><tbody><tr><td><strong>Low<\/strong><\/td><td><br><\/td><td><br><\/td><td><br><\/td><td><strong>High<\/strong><\/td><\/tr><tr><td>6<\/td><td>7<\/td><td>8<\/td><td>9<\/td><td>10<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, we have to find the middle element in our array. We can do this by using the formula: (low + high) \/ 2. In this case, the middle element is 8.<br><\/p>\n\n\n\n<p>Our algorithm will compare whether the middle element is equal to the one for which we are searching. If the numbers are equal, our search can stop. We\u2019re looking for the number 7, which is not equal to 8, so our search continues.<br><\/p>\n\n\n\n<p>Our algorithm compares whether the number for which we are searching is greater than the middle number. If it is greater, our search will begin all over again on the top half of our list. This is performed by setting low to be equal to low = middle_number + 1. Otherwise, the search will start again on the bottom half of our list.<br><\/p>\n\n\n\n<p>7 (the number we\u2019re searching for) is not greater than 8 (the middle number). This means that our algorithm is going to search through the bottom half of our list. We do this by setting our \u201chigh\u201d value to high = middle_number &#8211; 1.<br><\/p>\n\n\n\n<figure class=\"wp-block-table course-info-table\"><table><tbody><tr><td><strong>Low<\/strong><\/td><td><strong>Middle<\/strong><\/td><td><strong>High<\/strong><\/td><td><br><\/td><td><br><\/td><\/tr><tr><td>6<\/td><td>7<\/td><td>8<\/td><td>9<\/td><td>10<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Now, our algorithm is going to repeat our search. We\u2019ll compare the middle number, 7, with the one for which we are searching. We are looking for the number 7, so our search algorithm stops. We\u2019ve found the position of 7 in our list!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-implement-a-java-binary-search\">How to Implement a Java Binary Search<\/h2>\n\n\n\n<p>We\u2019re done with the theory so now all that is left to do is implement our search. It\u2019s one thing going through and searching a list by ourselves; it\u2019s another thing entirely to code an algorithm that does the search for us.<br><\/p>\n\n\n\n<p>We\u2019ll start by writing a program that implements a Java binary search using the iterative method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-iterative-method\">Iterative Method<\/h3>\n\n\n\n<p>Let\u2019s define a function called <code>searchItems<\/code>, which searches through our list of items:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>class BinarySearch {\n\tint searchItems(int array[], int searchingFor, int low, int high) {\n\t\twhile (low &lt;= high) {\n\t\t\tint middle = low + (high - low) \/ 2;\n\t\t\tif (array[middle] == searchingFor) {\n\t\t\t\treturn middle;\n\t\t\t} else if (array[middle] &lt; searchingFor) {\n\t\t\t\tlow = middle + 1;\n\t\t\t} else {\n\t\t\t\thigh = middle - 1;\n\t\t\t}\n\t\t}\n\t\treturn -1;\n\t}\n}<\/pre><\/div>\n\n\n\n<p>This function searches through our list of items using the binary search.<br><\/p>\n\n\n\n<p>Our function accepts four parameters: the list through which we want to search (array), the item for which we are searching (searchingFor), the low number (low), and the high number (high).<br><\/p>\n\n\n\n<p>Inside our function we have declared a while loop. While the value of \u201clow\u201d is less than or equal to \u201chigh\u201d, the loop will execute.<br><\/p>\n\n\n\n<p>Our while loop begins by calculating the middle number. It then checks to see whether the value at that position is equal to the one for which we are searching.<br><\/p>\n\n\n\n<p>If those numbers are equal, that value is returned to our main program. If not, our program checks if the value at the middle position is less than the one for which we are searching. If it is, a new low value is set.<br><\/p>\n\n\n\n<p>Otherwise, a new high value is set so our list can search again in the upper half of the list.<br><\/p>\n\n\n\n<p>If our item cannot be found, -1 is returned. We\u2019ll use this to tell our main program that the list item cannot be found in a minute.<br><\/p>\n\n\n\n<p>Our code doesn\u2019t do anything yet because we haven\u2019t written our main program. Add the following code below the <code>searchItems()<\/code> function in your BinarySearch class:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>public static void main(String args[]) {\n\t\tBinarySearch newSearch = new BinarySearch();\n\t\tint listToSearch[] = { 6, 7, 8, 9, 10 };\n\t\tint listLength = listToSearch.length;\n\t\tint numberToFind = 7;\n\t\tint findNumber = newSearch.searchItems(listToSearch, numberToFind, 0, listLength - 1);\n\t\tif (findNumber != -1) {\n\t\t\tSystem.out.println(numberToFind + \" was found at index position \" + findNumber + \".\");\n\t\t} else {\n\t\t\tSystem.out.println(numberToFind + \" was not found in the list.\");\n\t\t}\n\t}<\/pre><\/div>\n\n\n\n<p>Let\u2019s run our code and see what happens:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>7 was found at index position 1.\nWe've found the position of our number!<\/pre><\/div>\n\n\n\n<p>Our main program starts by initializing an instance of the BinarySearch class. We use this to start our search latre in the program. We then define three variables:<br><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>listToSearch: The list through which we want to search.<\/li>\n\n\n\n<li>listLength: The length of the list.<\/li>\n\n\n\n<li>numberToFind: The number that we\u2019re looking for in the list.<\/li>\n<\/ul>\n\n\n\n<p>Once we have defined these variables, we use the <code>searchItems()<\/code> function we declared earlier to find a number in our list. We assign the value this function returns to the variable \u201cfindNumber\u201d.<br><\/p>\n\n\n\n<p>If findNumber is not equal to -1 \u2013 which means our number has been found \u2013 then the index position of the item that we\u2019re searching for is printed to the console. Otherwise, a message is printed to the console telling us the number could not be found.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-recursive-method\">Recursive Method<\/h3>\n\n\n\n<p>A recursive approach can be taken to implement a binary search. This is where you write a function that calls itself until a specified item is found.<br><\/p>\n\n\n\n<p>Let\u2019s start by defining a function that performs our binary search recursively:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>class BinarySearch {\n\tint searchItems(int array[], int searchingFor, int low, int high) {\n\t\tif (high &gt;= low) {\n\t\t\tint middle = low + (high - low) \/ 2;\n\t\nif (array[middle] == searchingFor) {\n\t\t\t\treturn middle;\n\t\t\t} else if (array[middle] &lt; searchingFor) {\n\t\t\t\tsearchItems(array, searchingFor, middle + 1, high);\n\t\t\t} else {\n\t\t\t\tsearchItems(array, searchingFor, low, middle - 1);\n\t\t\t}\n\t\t}\n\t\treturn -1;\n\t}\n}<\/pre><\/div>\n\n\n\n<p>This function implements our binary search in a different way. Instead of using a while loop, we use an <code>if<\/code> statement to check if the high number is equal to or less than the low number.<br><\/p>\n\n\n\n<p>If this statement evaluates to true, our search begins. Otherwise, -1 is returned, which tells our program that a specified item could not be found in the list.<br><\/p>\n\n\n\n<p>Inside our <code>if<\/code> statement, we check if the value of the middle number is equal to the one for which we are searching. If it is, we return that number to our main program.<br><\/p>\n\n\n\n<p>If this statement doesn\u2019t evaluate to true, our program checks to see if the number in the middle position is less than the one for which we are searching. If it is, the <code>searchItems()<\/code> method will be run again, but this time our low number will be equal to one greater than our middle number. This divides the number of items our list needs to search through by two.<br><\/p>\n\n\n\n<p>Otherwise, <code>searchItems()<\/code> is run again and the value of the highest number is made equal to the middle number minus one. This means that we can refine our search only to the left half of our list.<br><\/p>\n\n\n\n<p>We can use the same main function that we wrote earlier to test out our code. Let\u2019s see what happens when we run our code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>7 was found at index position 1.<\/pre><\/div>\n\n\n\n<p>Our item was found again! This time, we used a recursive approach to finding our number instead of an iterative one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-complexity-analysis\">Complexity Analysis<\/h2>\n\n\n\n<p>The binary search algorithm has a best case complexity of O(1). This happens when the first item that is compared by the algorithm is the one that is being searched for.<\/p>\n\n\n\n<p>A binary search algorithm has an average and worst case complexity of O(log n). This means that, in most cases and in the worst case, the speed of the algorithm slows logarithmically depending on how many items the list has to search through.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<p>Binary searches are used to find the position of an element in a sorted list. Binary searches compare the item in the middle of a portion of an array to the number for which the algorithm is searching.<br><\/p>\n\n\n\n<p>Binary searches are more efficient than linear searches. This is because every time a search is performed the number of values that need to be searched through is divided by two.<br><\/p>\n\n\n\n<p>Now you\u2019re ready to implement a binary search in Java like an expert programmer.<\/p>\n","protected":false},"excerpt":{"rendered":"How to Write a Binary Search Algorithm in Java Computers do not search through items like humans do. Whereas humans can change their approach to finding something, computers need to be given specific instructions on how to locate a particular item. That\u2019s where standard algorithms are useful. Binary searches are an example of a standard&hellip;","protected":false},"author":240,"featured_media":19278,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17289],"tags":[],"class_list":{"0":"post-19277","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-java"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Java","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":"","is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Binary Search Java: A Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"A binary search is an efficient method of finding the position of an element in an array. On Career Karma, learn about binary searches 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:\/\/careerkarma.com\/blog\/binary-search-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Binary Search Java: A Guide\" \/>\n<meta property=\"og:description\" content=\"A binary search is an efficient method of finding the position of an element in an array. On Career Karma, learn about binary searches in Java.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/binary-search-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-10T21:33:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T11:53:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/wesley-tingey-snNHKZ-mGfE-unsplash.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-java\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-java\\\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Binary Search Java: A Guide\",\"datePublished\":\"2020-07-10T21:33:41+00:00\",\"dateModified\":\"2023-12-01T11:53:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-java\\\/\"},\"wordCount\":1529,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/wesley-tingey-snNHKZ-mGfE-unsplash.jpg\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-java\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-java\\\/\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-java\\\/\",\"name\":\"Binary Search Java: A Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-java\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/wesley-tingey-snNHKZ-mGfE-unsplash.jpg\",\"datePublished\":\"2020-07-10T21:33:41+00:00\",\"dateModified\":\"2023-12-01T11:53:33+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"A binary search is an efficient method of finding the position of an element in an array. On Career Karma, learn about binary searches in Java.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-java\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-java\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-java\\\/#primaryimage\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/wesley-tingey-snNHKZ-mGfE-unsplash.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/wesley-tingey-snNHKZ-mGfE-unsplash.jpg\",\"width\":1020,\"height\":680},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/binary-search-java\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Binary Search Java: A Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/author\\\/jamesgallagher\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Binary Search Java: A Guide | Career Karma","description":"A binary search is an efficient method of finding the position of an element in an array. On Career Karma, learn about binary searches 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:\/\/careerkarma.com\/blog\/binary-search-java\/","og_locale":"en_US","og_type":"article","og_title":"Binary Search Java: A Guide","og_description":"A binary search is an efficient method of finding the position of an element in an array. On Career Karma, learn about binary searches in Java.","og_url":"https:\/\/careerkarma.com\/blog\/binary-search-java\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-07-10T21:33:41+00:00","article_modified_time":"2023-12-01T11:53:33+00:00","og_image":[{"width":1020,"height":680,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/wesley-tingey-snNHKZ-mGfE-unsplash.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/binary-search-java\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-java\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Binary Search Java: A Guide","datePublished":"2020-07-10T21:33:41+00:00","dateModified":"2023-12-01T11:53:33+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-java\/"},"wordCount":1529,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-java\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/wesley-tingey-snNHKZ-mGfE-unsplash.jpg","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/binary-search-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/binary-search-java\/","url":"https:\/\/careerkarma.com\/blog\/binary-search-java\/","name":"Binary Search Java: A Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-java\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-java\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/wesley-tingey-snNHKZ-mGfE-unsplash.jpg","datePublished":"2020-07-10T21:33:41+00:00","dateModified":"2023-12-01T11:53:33+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"A binary search is an efficient method of finding the position of an element in an array. On Career Karma, learn about binary searches in Java.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/binary-search-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/binary-search-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/binary-search-java\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/wesley-tingey-snNHKZ-mGfE-unsplash.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/07\/wesley-tingey-snNHKZ-mGfE-unsplash.jpg","width":1020,"height":680},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/binary-search-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/careerkarma.com\/blog\/java\/"},{"@type":"ListItem","position":3,"name":"Binary Search Java: A Guide"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=19277"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/19277\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/19278"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=19277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=19277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=19277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}