Algorithm Binary Search
package myPackage;
import java.util.Scanner;
class ArrayQ {
int[] data;
int pj;
public ArrayQ(int ArrayQ) {
data = new int[ArrayQ];
pj = 0;
}
void insert(int angka) {
int i;
for (i = 0; i < pj; i++) {
if (data[i] > angka)
break;
}
for (int j = pj; j > i; j--) {
data[j] = data[j - 1];
}
data[i] = angka;
pj++;
}
void display() {
for (int i = 0; i < pj; i++)
System.out.print(data[i] + " ");
System.out.println();
}
void searching(int key) {
System.out.println("\nFind Value Process " + key);
int bawah = 0;
int tengah;
int atas = pj - 1;
while (atas >= bawah) {
tengah = (bawah + atas) / 2;
if (data[tengah] == key) {
System.out.println("Status : Found!!");
return;
} else {
if (data[tengah] < key)
bawah = tengah + 1;
else
atas = tengah - 1;
}
}
System.out.println("Status : not found!!");
}
}
public class BinarySearch2 {
public static void main(String[] args) {
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
System.out.println(" Binary Search\n ");
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
Scanner input = new Scanner(System.in);
System.out.print("Sum Value = ");
int jumlah = input.nextInt();
ArrayQ list;
list = new ArrayQ(sumValue);
System.out.println("input value :");
int[] val = new int[sumValue];
for (int i = 0; i < sumValue; i++) {
System.out.print(">> ");
val[i] = input.nextInt();
list.insert(val[i]);
}
list.display();
System.out.println("\nFind : ");
int search = input.nextInt();
list.searching(search);
// System.out.println(“Status : Found!!”);
// System.out.println(“Status : not Found!!”);
System.out.println();
}
}
good luck
ini apa ya neo???
ReplyDeleteitu merupakan algoritma pencarian dengan Binary
ReplyDelete