Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Binary Search java

package com.community.java;
/**
 * 
 * @author Akira
 */
public class ContohBinarySearch {
    private final int [] data = {5, 9, 12, 15, 17, 23, 27, 38, 42, 54, 64, 78, 90};
    
    private void tampilData(){
        for (int i : data) {
            System.out.print(i+" ");
        }
        System.out.println();
    }
    
    public String pencarianBinary(int key) {
        int bawah = 0;
        int atas = data.length - 1;

        while (atas >= bawah) {
            int tengah = (bawah + atas) / 2;
            if (key < data[tengah]){
                atas = tengah - 1;
            } else if (key == data[tengah]){
                return "Nomor "+key+" Berada Pada Urutan Ke - "+(tengah+1);
            }else{
                bawah = tengah + 1;
            }
        }
        return "Data Tidak Ditemukan";
    }
 
    public static void main(String args []){
        ContohBinarySearch obj = new ContohBinarySearch();
        obj.tampilData();
        System.out.println(obj.pencarianBinary(8));
    }

}
Source by www.community-java.com #
 
PREVIOUS NEXT
Tagged: #Binary #Search #java
ADD COMMENT
Topic
Name
5+5 =