Last active
December 17, 2015 08:09
-
-
Save agungsijawir/5578216 to your computer and use it in GitHub Desktop.
filename: Binarysearch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// filename: Binarysearch.java | |
package Binarysearch; | |
import java.util.Scanner; | |
public class Binarysearch { | |
public static void main(String[] args) { | |
int[] A={1,3,4,7,8,11,13,15,17,19}; | |
int cari, temu=0; | |
int lo = 0, lh = A.length - 1, mid = 0; | |
Scanner scan=new Scanner(System.in); | |
System.out.print("Find? "); | |
cari=scan.nextInt(); | |
while ( lo <= lh ) { | |
mid = (lo + lh) / 2; | |
if ( cari > A[mid] ) { lo = mid + 1; } | |
else if ( cari < A[mid] ) { lh = mid -1; } | |
else { temu = 1; break; } | |
} | |
if(temu==1)System.out.println("Nilai "+cari+" terdapat pada data ke-"+(mid+1)+" / A["+mid+"]"); | |
else System.out.println("Tidak ditemukan nilai "+cari+" pada himpunan data A"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment