document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.util.Arrays;
  2.  
  3. public class Binary_Search
  4. {
  5.     public static void main(String[] Args)
  6.     {
  7.        
  8.      
  9.         boolean notfound=true;
  10.         int[] arr = {0, 2, 5, 6, 9, 10, 15};
  11.        
  12.        
  13.          int target=15;
  14.        
  15.         int maxPos=arr.length-1;
  16.         int minPos=0;
  17.        
  18.         System.out.println("Data : " + Arrays.toString(arr));
  19.         while (notfound)
  20.         {
  21.             int curPos = (maxPos + minPos)/2;
  22.             if(arr[curPos] == target)
  23.             {
  24.                 notfound=false;
  25.                 System.out.println("Angka "+target+" ditemukan" );
  26.             }
  27.             else if(minPos>maxPos)
  28.             {
  29.             System.out.println("Angka " +target+" tidak ditemukan");
  30.             break;
  31.             }
  32.             else
  33.             {
  34.                 if(arr[curPos]< target)
  35.                 {
  36.                     minPos = curPos+1;
  37.                 }
  38.                 else
  39.                 {
  40.                     maxPos = curPos-1;
  41.                 }
  42.             }
  43.         }
  44.     }
  45. }
');