#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
//paramita 201383054
int main()
{
int a[1000],i,j,w;
cout<<"Mau masukkan berapa angka?: "; cin>>w;
cout<<"Input "<<w<<" Angka: "<<endl;
for(int i=0;i<w;i++)
{
cout<<"Angka ke "<<i+1<<": "; cin>>a[i];
}
cout<<"Angka sebelum diurutkan: "<<endl;
for (int i=0;i<w;i++)
{
cout<<a[i]<<", ";
}
for(int i=0;i<w;i++)
{
for(int j=i+1;j<w;j++)
{
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"\nAngka setelah diurutkan: "<<endl;
for (int i=0;i<w;i++)
{
cout<<a[i]<<", ";
}
int low=1, high=w, x, mid,q;
float l;
l=log2(w);
cout<<"\nMasukkan target yang dicari : "; cin>>x;
for (q=0;q<l; q++)
{
mid = ((low +high)/2);
if (a[mid-1] == x) {goto benar;}
else if (a[mid-1]<x) {low=mid+1;}
else if (a[mid-1]>x) {high=mid-1;}
}
mid = ((low +high)/2);
if (mid != x) {cout<<"\ndata ga ada"; goto salah;} getch();
benar:
cout<<"\nData ditemukan di urutan ke : "<<mid;
salah:
getch();
}
materi tentang binary search/ pencarian biner http://blog-sharings.blogspot.com/2012/07/konsep-binary-search.html
#include <conio.h>
#include <math.h>
using namespace std;
//paramita 201383054
int main()
{
int a[1000],i,j,w;
cout<<"Mau masukkan berapa angka?: "; cin>>w;
cout<<"Input "<<w<<" Angka: "<<endl;
for(int i=0;i<w;i++)
{
cout<<"Angka ke "<<i+1<<": "; cin>>a[i];
}
cout<<"Angka sebelum diurutkan: "<<endl;
for (int i=0;i<w;i++)
{
cout<<a[i]<<", ";
}
for(int i=0;i<w;i++)
{
for(int j=i+1;j<w;j++)
{
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"\nAngka setelah diurutkan: "<<endl;
for (int i=0;i<w;i++)
{
cout<<a[i]<<", ";
}
int low=1, high=w, x, mid,q;
float l;
l=log2(w);
cout<<"\nMasukkan target yang dicari : "; cin>>x;
for (q=0;q<l; q++)
{
mid = ((low +high)/2);
if (a[mid-1] == x) {goto benar;}
else if (a[mid-1]<x) {low=mid+1;}
else if (a[mid-1]>x) {high=mid-1;}
}
mid = ((low +high)/2);
if (mid != x) {cout<<"\ndata ga ada"; goto salah;} getch();
benar:
cout<<"\nData ditemukan di urutan ke : "<<mid;
salah:
getch();
}
materi tentang binary search/ pencarian biner http://blog-sharings.blogspot.com/2012/07/konsep-binary-search.html
Tidak ada komentar:
Posting Komentar