Binary Seacrh - dalam java

ini source program untuk pencarian biner dengan menggunakan bahasa java.

import java.io.*;

public class BinarySearch {

static int binSearch(int[] array, int val) {
int bawah = 0, atas = array.length;
while(bawah <= atas) { int mid = (bawah + atas)/2; if(array[mid] == val) { return mid; } else if(array[mid] > val) {
atas = mid - 1;
} else if(array[mid] < val) { bawah = mid + 1; } } return -1; } public static void main(String[] args) throws Exception { // TODO Auto-generated method stub int[] ArrayOfInts = {-17, 14, 19, 47, 49, 89, 147, 461, 571}; System.out.println("Binary Search Program\n"); System.out.println("array:"); for(int i = 0; i < ArrayOfInts.length; ++i) { System.out.println(" " + ArrayOfInts[i]); } BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("\nsearch: "); String buffer = in.readLine(); int nSearch = Integer.parseInt(buffer); int result = binSearch(ArrayOfInts, nSearch); if(result == -1) { System.out.println("tidak di temukan"); } else { System.out.println("ketemu!, ada diposisi = " + result); } } }

Comments

Popular posts from this blog

Shenzhen Hi-tec network Technology co. Ltd

Network Technology course

Network Technology book Pdf