program sequential
#include <cstdlib>
#include <iostream>
using namespace std;
int data[10];
int binary_search(int cari)
{
int l,r,m;
int n = 10;
l = 0;
r = n-1;
int ketemu = 0;
while(l<=r && ketemu==0)
{
m = (l+r)/2;
if( data[m] == cari )
ketemu = 1;
else
if (cari < data[m])
r = m-1;
else l = m+1;
}
if(ketemu == 1) return 1;
else return 0;
}
int main(int argc, char *argv[])
{
int cari,hasil;
for (int i = 0;i<10;i++){
cout<<"Masukkan datanya :";cin>>data[i];
}
for (int i = 0;i<10;i++){
cout<<data[i]<<""<<endl;
}
cout<<"masukkan data yang ingin dicari = ";
cin>>cari;
hasil = binary_search(cari);
if(hasil == 1)
{
cout<<"Data ada!"<<endl;
}
else
if(hasil == 0)
cout<<"Data Tidak ada!"<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Komentar
Posting Komentar