#include <iostream>
#include <conio.h>
using namespace std;
int binary_s(int array[], int size, int elemen);
int main()
{
int size=10;
int data[10]={18,17,16,11, 13, 14, 19, 15, 12 ,20} ;
cout<<"NAMA : MUHAMAD HUSNI RAMADHAN"<<endl;
cout<<"NIM : 191011400011 "<<endl;
cout<<"KELAS: 02TPLE002 "<<endl;
cout<<"Data Array"<<endl;
int i;
for(i=0;i<size;i++)
cout<<data[i]<<" ";
cout<<endl<<endl<<"masukkan data yang ingin anda cari: ";
int cari;
cin>>cari;
// pencarian
int hasil;
hasil = binary_s(data, size, cari);
if (hasil==0)
cout<<"Nilai ditemukan";
else
cout<<"Nilai tidak ditemukan";
getch();
}
int binary_s(int array[], int size, int elemen)
{
int awal = 0;
int akhir = size-1;
int nilaiTengah = (awal+akhir)/2;
while (nilaiTengah<=size && awal<=akhir)
{
nilaiTengah = (awal+akhir)/2;
if (array[nilaiTengah]==elemen)
return 1;
else if (elemen<array[nilaiTengah])
akhir = nilaiTengah-1;
else
awal = nilaiTengah+1;
}
return 0;
}
Tidak ada komentar:
Posting Komentar