#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
float
array[100]={1,7,5,9,8,6,4,3,10,11,12,14,2};
int selisih, cari, kiri, kanan, tng;
int a, b, tukar;
cout<<"angka sebelum diurutkan =
";
for (a = 0; a < 13; a++)
{
cout<<array[a]<<" ";
}
for (a=0;a<12;a++)
{
for (b=0;b<13-a-1;b++)
{
if (array[b]>array[b+1])
{
tukar = array[b];
array[b] = array[b+1];
array[b+1] = tukar;
}
}
}
cout<<"\n\nangka setelah diurutkan =
";
for (a=0;a<13;a++ )
{
cout<<array[a]<<" ";
}
cari = 2;
kiri = 0;
kanan = 12;
tng = kanan/2;
cout<<"\n\nData yang dicari :
"<<cari<<"\n"<<endl;
do
{
if (cari<array[tng])
{
kanan = tng-1;
selisih = kanan-kiri;
if (selisih == 1)
{
tng = kanan;
}
else
{
tng =kanan/2;
}
}
else if (cari>array[tng])
{
kiri =tng+1;
tng =(kanan/4)+(kanan/2);
}
}while(cari == tng);
if (array[tng] == cari)
{
cout<<"Data telah ditemukan";
}
else
{
cout<<"Data tidak dapat
ditemukan";
}
getch();
}
No comments:
Post a Comment