Selasa, 17 Mei 2016

Contoh Program Searching (Binary Search dan Sequential Search) bahasa Java




1. Binary Search
  • Pencarian data dimulai dari pertengahan data yang telah terurut.
  • Jika kunci pencarian lebih kecil daripada kunci posisi tengah, maka kurangi lingkup pencarian pada separuh data pertama.
  • Begitu juga sebaliknya jika kunci pencarian lebih besar daripada kunci tengah, maka pencarian ke separuh data kedua.
  • Teknik Binary Search hanya dapat digunakan pada sorted array.
berikut souurce code keseluruhan dari binary search :

import java.util.Scanner;                                                                                                  
public class Binary {                                                                                                        


    /**                                                                                                                             
     * @param args the command line arguments                                                              
     *                                                                                                                               
    public static void main(String[] args) {                                                                         
           int N = 8;                                                                                                            
    int A [] = {5,2,9,7,1,6,8,3};                                                                                       
    int BatasAtas, BatasBawah, Tengah;                                                                           
    int cari = 2;                                                                                                                
    BatasAtas = 0;                                                                                                          
    BatasBawah = N - 1;                                                                                                
    Tengah = 0;                                                                                                               
    boolean ketemu;                                                                                                        
    ketemu = false;                                                                                                          

    while((BatasAtas <= BatasBawah) && (ketemu == false)){                                       

        Tengah = (BatasAtas + BatasBawah) / 2;                                                               
        if (A[Tengah] == cari){                                                                                          
            ketemu = true;                                                                                                   
        } else                                                                                                                    
            if (A[Tengah] <= cari){                                                                                      
            BatasAtas = Tengah + 1;                                                                                   
        } else {                                                                                                                 
            BatasBawah = Tengah - 1;                                                                                
        }                                                                                                                           
        }                                                                                                                           
    if (ketemu) {                                                                                                              
        System.out.println ("Angka : "+ cari + "Data berada di index nomor " + Tengah);     
    } else {                                                                                                                      
        System.out.println ("Angka :" + cari + "Data tidak ditemukan");                               
    }                                                                                                      
                          


2. Sequential Search
  • Merupakan teknik yang sederhana dan langsung dapat digunakan pada struktur data array.
  • Pencarian data secara urut mulai dari data pertama sampai kunci yang dicari ditemukan atau sampai seluruh data telah dicari dan tidak ditemukan
  •  Dilakukan pada data yang tidak terurut
Berikut Source code keseluruhan Sequential Search pada bahasa pemrograman java :

public class Sequential {                                                                                           

    /**                                                                                                                      
     * @param args the command line arguments                                                       
     */                                                                                                                       
    public static void main(String[] args) {                                                                  
                int[] data = {99,20,17,8,27,5,21,10,41,11};                                            
        int cari=8;                                                                                                        
        int i=0;                                                                                                             
        boolean ditemukan = false;                                                                               

        for(i=0; i < data.length; i++){                                                                           
            if (data[i] == cari){                                                                                      
                ditemukan=true;                                                                                      
                break;                                                                                                     
            }                                                                                                                 
        }                                                                                                                     
        if (ditemukan) {                                                                                               
            System.out.println("Data : " + cari + " Ditemukan Pada Index :"+ i +".");       
        }                                                                                                                     
        else{                                                                                                                
            System.out.println("Data Tidak di temukan");                                                
        }                                                                                                                     

    }                                                                                                                         

}                                                                                                                             


*Sekian Postingan dari contoh program Searching (Binary dan Sequential) apabila ada kesalahan atau karna kurang lengkap penjelasannya mohon maaf. Terima Kasih atas kunjungannya ^_^ 

Source By : wendigusfan

0 komentar:

Posting Komentar