PROGRAM BINARY SEARCH DALAM BAHASA DEV C++
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int data[9]={3,9,11,12,15,17,23,31,35};
int l,r,m;
int n=9;
int cari=17;
l=0;
r=n-1;
int ktm=0;
while(l<=r && ktm==0)
{
m=(l+r)/2;
cout<<"data tengah:"<<m<<endl;
if(data[m]==cari)
ktm=1;
else if(cari<data[m])
{
cout<<"cari dikiri"<<endl;
r=m-1;
}
else
{
l=m+1;
cout<<"cari dikanan"<<endl;
}
if(ktm==1)
cout<<"data ada"<<endl;
else
cout<<"data tidak ada"<<endl;
system ("PAUSE");
return 0;
}
}
//tugas yogi pratama
Tidak ada komentar:
Posting Komentar