Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class Binary_Search
- {
- public static void main(String[] Args)
- {
- boolean notfound=true;
- int[] arr = {0, 2, 5, 6, 9, 10, 15};
- int target=15;
- int maxPos=arr.length-1;
- int minPos=0;
- System.out.println("Data : " + Arrays.toString(arr));
- while (notfound)
- {
- int curPos = (maxPos + minPos)/2;
- if(arr[curPos] == target)
- {
- notfound=false;
- System.out.println("Angka "+target+" ditemukan" );
- }
- else if(minPos>maxPos)
- {
- System.out.println("Angka " +target+" tidak ditemukan");
- break;
- }
- else
- {
- if(arr[curPos]< target)
- {
- minPos = curPos+1;
- }
- else
- {
- maxPos = curPos-1;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment