fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static boolean exists(int[] arrayToScan, int valueToFind) {
  11. for (int startIndex = 0, endIndex = arrayToScan.length, middleIndex = endIndex / 2;
  12. startIndex < endIndex;
  13. middleIndex = startIndex + (endIndex - startIndex) / 2) {
  14. if (arrayToScan[middleIndex] == valueToFind) {
  15. return true;
  16. }
  17. if (valueToFind > arrayToScan[middleIndex]) {
  18. startIndex = middleIndex + 1;
  19. } else {
  20. endIndex = middleIndex;
  21. }
  22. }
  23. return false;
  24. }
  25.  
  26. public static boolean find(int[] arrayToScan, int valueToFind) {
  27. int startIndex = 0, endIndex = arrayToScan.length, midleIndex;
  28. while (true) {
  29. midleIndex = (startIndex + endIndex) / 2;
  30. if (arrayToScan[midleIndex] == valueToFind) {
  31. return true;
  32. }
  33. if (startIndex >= endIndex || midleIndex == 0 || midleIndex == arrayToScan.length - 1) {
  34. return false;
  35. }
  36. if (valueToFind > arrayToScan[midleIndex]) {
  37. startIndex = midleIndex+1 ;
  38. }
  39. if (valueToFind < arrayToScan[midleIndex]) {
  40. endIndex = midleIndex-1;
  41. }
  42.  
  43. }
  44. }
  45.  
  46. public static void main(String[] args) {
  47. for (int sz = 1; sz < 20; sz++) {
  48. int[] data = new int[sz];
  49. for (int i = 0; i < sz; i++) {
  50. data[i] = i;
  51. }
  52. for (int i = 0; i < sz; i++) {
  53. System.out.printf("Array len %3d try %3d find %b exists %b\n", sz, i, find(data, i), exists(data, i));
  54. }
  55. }
  56. }
  57.  
  58. }
Success #stdin #stdout 0.09s 711168KB
stdin
Standard input is empty
stdout
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