Iterative Binary Search Algorithm for Coding Interviews
Tags: beginners, programming, java, tutorial
Published: 2019-04-14 05:17:18

---
title: Iterative Binary Search Algorithm for Coding Interviews
published: true
description: A non-recursive implementation of the classical binary search algorithm in Java. 
tags: beginners,programming,java,tutorial
---
*Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.*

[![](https://thepracticaldev.s3.amazonaws.com/i/dab02oapz46l90464wfe.jpg)](https://www.java67.com/2019/07/top-10-online-courses-to-learn-data-structure-and-algorithms-in-java.html)

Hello everyone! I have published a lot of algorithms and data structure articles from coding interviews on [my blog](http://javarevisited.blogspot.com/), but this one is one of the first ones here. In this article, we'll examine popular [fundamental algorithms for interviews](http://www.java67.com/2018/06/data-structure-and-algorithm-interview-questions-programmers.html).


Yes, you guessed it right: I am talking about  **binary search** algorithm, one of the fundamental search algorithm which is neither too easy nor too tough. This algorithm is very easy to implement with [Recursion](https://www.educative.io/collection/page/10370001/760001/1000001?affiliate_id=5073518643380224) and that's why most of the interviewer will force you to implement a binary search algorithm without recursion, also known as iterative binary search and that's what you will learn in this tutorial. 

In computer science, a binary search, or half-interval search, is a **divide and conquer algorithm** that locates the position of an item in a [sorted array](http://www.java67.com/2014/12/how-to-find-missing-number-in-sorted.html). Binary searching works by comparing an input value to the middle element of the array.

The comparison determines whether the element equals the input, is less than the input, or is greater than the input.

When the element being compared equals the input, the search stops and typically returns the position of the element.

If the element is not equal to the input, then a comparison is made to determine whether the input is less than or greater than the element.

Depending on the result, the [algorithm](https://javarevisited.blogspot.com/2018/11/top-5-data-structures-and-algorithm-online-courses.html#axzz5YFaOvjsh) then starts over again, but only searching the top or a bottom subset of the array's elements.

If the input is not located within the [array](https://javarevisited.blogspot.com/2015/06/3-ways-to-find-duplicate-elements-in-array-java.html), the algorithm will usually output a unique value indicating this like -1 or just throw a [RuntimeException](http://www.java67.com/2012/12/difference-between-runtimeexception-and-checked-exception.html) in Java like NoValueFoundException.

Binary search algorithms typically halve the number of items to check with each successive iteration, thus locating the given item (or determining its absence) in logarithmic time.

Btw, if you are not familiar with fundamental search and sort algorithms, then you can also join a course like [**Data Structures and Algorithms: Deep Dive Using Java**](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fdata-structures-and-algorithms-deep-dive-using-java%2F) to learn fundamental algorithms.

[![](https://cdn-images-1.medium.com/max/1000/1*odNs-yx5RYj8Vdhzm2GEsA.png)](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fdata-structures-and-algorithms-deep-dive-using-java%2F)

If Java is not your choice of language, you can find more recommendations for JavaScript and Python in this [list of algorithms courses](https://javarevisited.blogspot.com/2018/11/top-5-data-structures-and-algorithm-online-courses.html#axzz5YFaOvjsh).

Btw, if you prefer books, I suggest you read a comprehensive algorithm book like [**Introduction to Algorithms**](http://www.amazon.com/dp/0072970545/?tag=javamysqlanta-20) by Thomas H. Cormen.


Here is some sample code that shows the logic of **iterative binary search in Java**:

[![](https://cdn-images-1.medium.com/max/1000/0*-BoLwPTypf2kCkXg.jpg)](https://pluralsight.pxf.io/c/1193463/424552/7490?u=https%3A%2F%2Fwww.pluralsight.com%2Fcourses%2Fads-part1)




### Binary Search without Recursion in Java

Here is a sample program to implement binary search in Java. The algorithm is implemented recursively. Also, an interesting fact to know about binary search implementation in Java is that Joshua Bloch, author of the famous [Effective Java](https://www.amazon.com/Effective-Java-3rd-Joshua-Bloch/dp/0134685997/?tag=javamysqlanta-20) book, wrote the binary search in "java.util.Arrays".

```java
import java.util.Arrays;
import java.util.Scanner;

/**
 * Java program to implement Binary Search. We have implemented Iterative
 * version of Binary Search Algorithm in Java
 *
 * @author Javin Paul
 */

public class IterativeBinarySearch {

  public static void main(String args[]) {
    int[] list = new int[] { 23, 43, 31, 12 };
    int number = 12;
    Arrays.sort(list);
    System.out.printf("Binary Search %d in integer array %s %n", number,
        Arrays.toString(list));

    binarySearch(list, 12);
    System.out.printf("Binary Search %d in integer array %s %n", 43,
        Arrays.toString(list));

    binarySearch(list, 43);
    list = new int[] { 123, 243, 331, 1298 };
    number = 331;
    Arrays.sort(list);
    System.out.printf("Binary Search %d in integer array %s %n", number,
        Arrays.toString(list));

    binarySearch(list, 331);
    System.out.printf("Binary Search %d in integer array %s %n", 331,
        Arrays.toString(list));
    binarySearch(list, 1333);

    // Using Core Java API and Collection framework
    // Precondition to the Arrays.binarySearch
    Arrays.sort(list);

    // Search an element
    int index = Arrays.binarySearch(list, 3);

  }

  /**
   * Perform a binary Search in Sorted Array in Java
   * 
   * @param input
   * @param number
   * @return location of element in array
   */

  public static void binarySearch(int[] input, int number) {
    int first = 0;
    int last = input.length - 1;
    int middle = (first + last) / 2;

    while (first <= last) {
      if (input[middle] < number) {
        first = middle + 1;
      } else if (input[middle] == number) {

        System.out.printf(number + " found at location %d %n", middle);
        break;
      } else {
        last = middle - 1;
      }

      middle = (first + last) / 2;

    }

    if (first > last) {
      System.out.println(number + " is not present in the list.\n");
    }

  }
}

**Output**
Binary Search 12 in integer array [12, 23, 31, 43]
12 found at location 0
Binary Search 43 in integer array [12, 23, 31, 43]
43 found at location 3
Binary Search 331 in integer array [123, 243, 331, 1298]
331 found at location 2
Binary Search 331 in integer array [123, 243, 331, 1298]
1333 is not present in the list.

```


That's all about **how to implement binary search without using recursion in Java**. Along with Linear search, these are two of the essential search algorithms you should have learned in your computer science class. 

These are not just important from a Coding interview perspective but also to understand other essential data structures like [binary search tree or BST](https://javarevisited.blogspot.com/2015/10/how-to-implement-binary-search-tree-in-java-example.html).

The binary search tree data structure takes advantage of this algorithm and arranges data in a hierarchical structure so that you can search any node in O(logN) time.

Though, you must remember that in order to use binary search, you need a [sorted list](https://www.java67.com/2012/07/sort-list-ascending-descending-order-set-arraylist.html) or [sorted array](https://www.java67.com/2014/08/4-examples-to-sort-array-in-java.html), so you also need to consider the cost of sorting when you consider using binary search algorithms in the real world.


 **Further Learning**
 [Data Structures and Algorithms: Deep Dive Using Java](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fdata-structures-and-algorithms-deep-dive-using-java%2F)
 [Algorithms and Data Structures --- Part 1 and 2](https://pluralsight.pxf.io/c/1193463/424552/7490?u=https%3A%2F%2Fwww.pluralsight.com%2Fcourses%2Fads-part1)
 [Data Structures in Java 9 by Heinz Kabutz](https://learning.javaspecialists.eu/courses/data-structures?affcode=92815_johrd7r8)
[10 Algorithms books for Interviews](http://www.java67.com/2015/09/top-10-algorithm-books-every-programmer-read-learn.html)
[10 Data Structure and Algorithm Courses for Interviews](https://hackernoon.com/10-data-structure-algorithms-and-programming-courses-to-crack-any-coding-interview-e1c50b30b927)
[5 Free Courses to Learn Data Structure and Algorithms](https://javarevisited.blogspot.com/2018/01/top-5-free-data-structure-and-algorithm-courses-java--c-programmers.html)
 [Data Structures in Java: An Interview Refresher](https://www.educative.io/collection/5642554087309312/5724822843686912?affiliate_id=5073518643380224)

Other **Data Structure and Algorithms Problems** from Programming Job interviews

-   How to implement the Quicksort algorithm in place in Java? ([tutorial](http://javarevisited.blogspot.sg/2014/08/quicksort-sorting-algorithm-in-java-in-place-example.html))
-   How to implement Binary Search Tree in Java? ([solution](http://javarevisited.blogspot.sg/2015/10/how-to-implement-binary-search-tree-in-java-example.html))
-   How to implement Quicksort algorithm without recursion? ([tutorial](http://javarevisited.blogspot.sg/2016/09/iterative-quicksort-example-in-java-without-recursion.html))
-   How to implement the Insertion sort algorithm in Java? ([tutorial](http://javarevisited.blogspot.sg/2014/12/insertion-sort-algorithm-in-java-to-array-example.html))
-   How to implement a Bubble sort algorithm in Java? ([tutorial](http://www.java67.com/2012/12/bubble-sort-in-java-program-to-sort-integer-array-example.html))
-   What is the difference between Comparison and Non-Comparison based sorting algorithms? ([answer](http://javarevisited.blogspot.sg/2017/02/difference-between-comparison-quicksort-and-non-comparison-counting-sort-algorithms.html))
-   How to implement Bucket Sort in Java? ([tutorial](http://javarevisited.blogspot.sg/2017/01/bucket-sort-in-java-with-example.html))
-   How to implement a Binary Search Algorithm without recursion in Java? ([tutorial](http://www.java67.com/2016/05/java-program-to-perform-binary-search-without-recursion.html))
-   50+ Data Structure and Algorithms Courses for Programmers ([questions](https://dev.to/javinpaul/50-data-structure-and-algorithms-problems-from-coding-interviews-4lh2))

Thanks for reading this article. If you like this article then please share it with your friends and colleagues. If you have any suggestions or feedback then please drop a comment.

>P.S. - If you are serious about improving your Algorithm skills, I think the [Data Structures and Algorithms: Deep Dive Using Java](https://click.linksynergy.com/fs-bin/click?id=JVFxdTr9V80&subid=0&offerid=323058.1&type=10&tmpid=14538&RD_PARM1=https%3A%2F%2Fwww.udemy.com%2Fdata-structures-and-algorithms-deep-dive-using-java%2F) is the best one to start with.