/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static boolean exists(int[] arrayToScan, int valueToFind) { for (int startIndex = 0, endIndex = arrayToScan.length, middleIndex = endIndex / 2; startIndex < endIndex; middleIndex = startIndex + (endIndex - startIndex) / 2) { if (arrayToScan[middleIndex] == valueToFind) { return true; } if (valueToFind > arrayToScan[middleIndex]) { startIndex = middleIndex + 1; } else { endIndex = middleIndex; } } return false; } public static boolean find(int[] arrayToScan, int valueToFind) { int startIndex = 0, endIndex = arrayToScan.length, midleIndex; while (true) { midleIndex = (startIndex + endIndex) / 2; if (arrayToScan[midleIndex] == valueToFind) { return true; } if (startIndex >= endIndex || midleIndex == 0 || midleIndex == arrayToScan.length - 1) { return false; } if (valueToFind > arrayToScan[midleIndex]) { startIndex = midleIndex+1 ; } if (valueToFind < arrayToScan[midleIndex]) { endIndex = midleIndex-1; } } } for (int sz = 1; sz < 20; sz++) { int[] data = new int[sz]; for (int i = 0; i < sz; i++) { data[i] = i; } for (int i = 0; i < sz; i++) { System.out.printf("Array len %3d try %3d find %b exists %b\n", sz, i, find(data, i), exists(data, i)); } } } }
Standard input is empty
Array len 1 try 0 find true exists true Array len 2 try 0 find false exists true Array len 2 try 1 find true exists true Array len 3 try 0 find true exists true Array len 3 try 1 find true exists true Array len 3 try 2 find true exists true Array len 4 try 0 find true exists true Array len 4 try 1 find false exists true Array len 4 try 2 find true exists true Array len 4 try 3 find true exists true Array len 5 try 0 find true exists true Array len 5 try 1 find false exists true Array len 5 try 2 find true exists true Array len 5 try 3 find false exists true Array len 5 try 4 find true exists true Array len 6 try 0 find true exists true Array len 6 try 1 find true exists true Array len 6 try 2 find true exists true Array len 6 try 3 find true exists true Array len 6 try 4 find false exists true Array len 6 try 5 find true exists true Array len 7 try 0 find true exists true Array len 7 try 1 find true exists true Array len 7 try 2 find true exists true Array len 7 try 3 find true exists true Array len 7 try 4 find true exists true Array len 7 try 5 find true exists true Array len 7 try 6 find true exists true Array len 8 try 0 find true exists true Array len 8 try 1 find true exists true Array len 8 try 2 find true exists true Array len 8 try 3 find true exists true Array len 8 try 4 find true exists true Array len 8 try 5 find true exists true Array len 8 try 6 find true exists true Array len 8 try 7 find true exists true Array len 9 try 0 find true exists true Array len 9 try 1 find true exists true Array len 9 try 2 find true exists true Array len 9 try 3 find true exists true Array len 9 try 4 find true exists true Array len 9 try 5 find true exists true Array len 9 try 6 find true exists true Array len 9 try 7 find true exists true Array len 9 try 8 find true exists true Array len 10 try 0 find true exists true Array len 10 try 1 find false exists true Array len 10 try 2 find true exists true Array len 10 try 3 find true exists true Array len 10 try 4 find true exists true Array len 10 try 5 find true exists true Array len 10 try 6 find true exists true Array len 10 try 7 find true exists true Array len 10 try 8 find true exists true Array len 10 try 9 find true exists true Array len 11 try 0 find true exists true Array len 11 try 1 find false exists true Array len 11 try 2 find true exists true Array len 11 try 3 find true exists true Array len 11 try 4 find true exists true Array len 11 try 5 find true exists true Array len 11 try 6 find true exists true Array len 11 try 7 find true exists true Array len 11 try 8 find true exists true Array len 11 try 9 find false exists true Array len 11 try 10 find true exists true Array len 12 try 0 find true exists true Array len 12 try 1 find false exists true Array len 12 try 2 find true exists true Array len 12 try 3 find true exists true Array len 12 try 4 find true exists true Array len 12 try 5 find true exists true Array len 12 try 6 find true exists true Array len 12 try 7 find true exists true Array len 12 try 8 find true exists true Array len 12 try 9 find true exists true Array len 12 try 10 find false exists true Array len 12 try 11 find true exists true Array len 13 try 0 find true exists true Array len 13 try 1 find false exists true Array len 13 try 2 find true exists true Array len 13 try 3 find true exists true Array len 13 try 4 find true exists true Array len 13 try 5 find true exists true Array len 13 try 6 find true exists true Array len 13 try 7 find true exists true Array len 13 try 8 find true exists true Array len 13 try 9 find true exists true Array len 13 try 10 find true exists true Array len 13 try 11 find false exists true Array len 13 try 12 find true exists true Array len 14 try 0 find true exists true Array len 14 try 1 find true exists true Array len 14 try 2 find true exists true Array len 14 try 3 find true exists true Array len 14 try 4 find true exists true Array len 14 try 5 find true exists true Array len 14 try 6 find true exists true Array len 14 try 7 find true exists true Array len 14 try 8 find true exists true Array len 14 try 9 find true exists true Array len 14 try 10 find true exists true Array len 14 try 11 find true exists true Array len 14 try 12 find false exists true Array len 14 try 13 find true exists true Array len 15 try 0 find true exists true Array len 15 try 1 find true exists true Array len 15 try 2 find true exists true Array len 15 try 3 find true exists true Array len 15 try 4 find true exists true Array len 15 try 5 find true exists true Array len 15 try 6 find true exists true Array len 15 try 7 find true exists true Array len 15 try 8 find true exists true Array len 15 try 9 find true exists true Array len 15 try 10 find true exists true Array len 15 try 11 find true exists true Array len 15 try 12 find true exists true Array len 15 try 13 find true exists true Array len 15 try 14 find true exists true Array len 16 try 0 find true exists true Array len 16 try 1 find true exists true Array len 16 try 2 find true exists true Array len 16 try 3 find true exists true Array len 16 try 4 find true exists true Array len 16 try 5 find true exists true Array len 16 try 6 find true exists true Array len 16 try 7 find true exists true Array len 16 try 8 find true exists true Array len 16 try 9 find true exists true Array len 16 try 10 find true exists true Array len 16 try 11 find true exists true Array len 16 try 12 find true exists true Array len 16 try 13 find true exists true Array len 16 try 14 find true exists true Array len 16 try 15 find true exists true Array len 17 try 0 find true exists true Array len 17 try 1 find true exists true Array len 17 try 2 find true exists true Array len 17 try 3 find true exists true Array len 17 try 4 find true exists true Array len 17 try 5 find true exists true Array len 17 try 6 find true exists true Array len 17 try 7 find true exists true Array len 17 try 8 find true exists true Array len 17 try 9 find true exists true Array len 17 try 10 find true exists true Array len 17 try 11 find true exists true Array len 17 try 12 find true exists true Array len 17 try 13 find true exists true Array len 17 try 14 find true exists true Array len 17 try 15 find true exists true Array len 17 try 16 find true exists true Array len 18 try 0 find true exists true Array len 18 try 1 find true exists true Array len 18 try 2 find true exists true Array len 18 try 3 find true exists true Array len 18 try 4 find true exists true Array len 18 try 5 find true exists true Array len 18 try 6 find true exists true Array len 18 try 7 find true exists true Array len 18 try 8 find true exists true Array len 18 try 9 find true exists true Array len 18 try 10 find true exists true Array len 18 try 11 find true exists true Array len 18 try 12 find true exists true Array len 18 try 13 find true exists true Array len 18 try 14 find true exists true Array len 18 try 15 find true exists true Array len 18 try 16 find true exists true Array len 18 try 17 find true exists true Array len 19 try 0 find true exists true Array len 19 try 1 find true exists true Array len 19 try 2 find true exists true Array len 19 try 3 find true exists true Array len 19 try 4 find true exists true Array len 19 try 5 find true exists true Array len 19 try 6 find true exists true Array len 19 try 7 find true exists true Array len 19 try 8 find true exists true Array len 19 try 9 find true exists true Array len 19 try 10 find true exists true Array len 19 try 11 find true exists true Array len 19 try 12 find true exists true Array len 19 try 13 find true exists true Array len 19 try 14 find true exists true Array len 19 try 15 find true exists true Array len 19 try 16 find true exists true Array len 19 try 17 find true exists true Array len 19 try 18 find true exists true