Selasa, 23 Juli 2013

Membuat struktur data dengan binary search II


 Berikut source code nya:




/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package binarysearch;

import javax.swing.JOptionPane;

/**
 *
 * @author MACHAAPIEDH
 */
public class BinarySearch {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
      int angka;
        int [] numb = new int [6];
        int key =Integer.parseInt(JOptionPane.showInputDialog("masukkan angka :"));
        System.out.println("key ="+key);
        System.out.println("inputan angka");
        for (int i=0; i<numb.length;i++){
            angka = Integer.parseInt(JOptionPane.showInputDialog("masukkan angka ke-"+(i+1)+"."));
            numb [i]=angka;
            System.out.print(angka+" ");
        }
     

       }
  
    public int binarySearch(int numb[],int key){
        int low = 0;
        int high = numb.length-1;
        while(low >=high){
            int mid = (low+high)/2;
            if(numb[mid]==key){
                return mid;
            }else if(numb[mid]<key)
                    low = mid + 1;
                    else
                    high = mid -1;
        }
        return -1;                    
}  
}

Penampakan: 
 

Tidak ada komentar:

Posting Komentar

Entri Populer