Binary search with each step illustrated in Java

Description

The following code shows how to binary search with each step illustrated.

Example


// www . j a  va  2  s .com
import java.util.Arrays;
import java.util.Random;

public class Main {
  private static int[] data = new int[10]; // create space for array

  public static void main(String[] args) {
    Random generator = new Random();

    for (int i = 0; i < 10; i++)
      data[i] = 10 + generator.nextInt(90);

    Arrays.sort(data);

    System.out.println(binarySearch(3));
  }

  public static int binarySearch(int searchElement) {
    int low = 0;
    int high = data.length - 1;
    int middle = (low + high + 1) / 2;
    int location = -1;

    do {
      System.out.print(remainingElements(low, high));

      for (int i = 0; i < middle; i++)
        System.out.print("   ");
      System.out.println(" * ");

      if (searchElement == data[middle])
        location = middle;
      else if (searchElement < data[middle])
        high = middle - 1;
      else
        low = middle + 1;

      middle = (low + high + 1) / 2;
    } while ((low <= high) && (location == -1));

    return location;
  }

  public static String remainingElements(int low, int high) {
    StringBuffer temporary = new StringBuffer();

    for (int i = 0; i < low; i++)
      temporary.append("   ");

    for (int i = low; i <= high; i++)
      temporary.append(data[i] + " ");

    temporary.append("\n");
    return temporary.toString();
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Development »




Java Algorithms
Java Clipboard
Java Compiler
Java Desktop
Java Virtual Machine
Java Math
OS
Random
Java Robot
Java RuntimeMXBean
Java Timer
Java UUID
Java Internationalization