1 import java.util.Arrays;
2 import java.util.Scanner;
3
4 /**
5 This program demonstrates the binary search algorithm.
6 */
7 public class BinarySearchDemo
8 {
9 public static void main(String[] args)
10 {
11 // Construct random array
12
13 int[] a = ArrayUtil.randomIntArray(20, 100);
14 Arrays.sort(a);
15 System.out.println(Arrays.toString(a));
16 BinarySearcher searcher = new BinarySearcher(a);
17 Scanner in = new Scanner(System.in);
18
19 boolean done = false;
20 while (!done)
21 {
22 System.out.print
23 ("Enter number to search for, -1 to quit:");
24 int n = in.nextInt();
25 if (n == -1)
26 done = true;
27 else
28 {
29 int pos = searcher.search(n);
30 System.out.println("Found in position " + pos);
31 }
32 }
33 }
34 }