SHARE
TWEET

BINARY SEARCH

Qlynx Nov 19th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int cariBagiDua(int A[], int p, int bil){
  5.     int s=0;
  6.     int e=p-1;
  7.     int indeks = (s+e)/2;
  8.    
  9.     //Algoritma Bagi Dua
  10.     //Apakah Ketemu? Jika Ketemu Berakhir
  11.     while ((A[indeks]!=bil) && (s !=e)){
  12.         //Perikas Ke Kanan atau Ke Kiri
  13.         if (bil > A[indeks]){
  14.             //Ke Kiri
  15.             e=indeks;
  16.         }else{
  17.             //Ke Kanan
  18.             s=indeks;
  19.         }
  20.         indeks=(s+e)/2;
  21.     }
  22.     //Pengujian Terakhir
  23.     if(A[indeks] !=bil){
  24.         indeks = -1;
  25.     }
  26.    
  27.     return indeks;
  28. }
  29.  
  30. int main(){
  31.     const int PanjangLarik=11;
  32.     int A[PanjangLarik]={99,87,76,68,55,46,38,27,18,15,9};
  33.    
  34.     int bil, indeks;
  35.     cout << "Cari Indeks Dari = "; cin >> bil;
  36.     indeks = cariBagiDua (A,PanjangLarik,bil);
  37.     cout << "Ada di Indeks Ke "<< indeks << endl;
  38.    
  39.     return 0;
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top