PROGRAM SEARCHING Spesial
#include <iostream>
#include <conio>
int data[10], data2[10];
int jml_index;
void tukar (int a, int b)
{
int temp;
temp = data[b];
data[b] = data[a];
data[a] = temp;
}
void tampil (int data[])
{
cout<<" : ";
for(int i=0; i<jml_index; i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
void SelectionSortASC()
{
cout<<"Sebelum Selection Sort ASC adalah ";tampil(data);
int posisi,i,j;
for(i=0; i<jml_index-1; i++)
{
posisi = i;
for(j=i+1; j<jml_index; j++)
{
if(data[j] < data [posisi])
{posisi = j;}
}
if(posisi != i)
{
tukar(posisi,i);
}
}
cout<<"Setelah Selection Sort ASC adalah ";tampil(data);
}
void Input()
{
cout<<endl;
cout<<"===================================="<<endl;
cout<<"Inputkan Jumlah Data (1 s/d 10 ) : ";
cin>>jml_index;
if(jml_index<1 || jml_index>10)
{
cout<<"Jumlah Yang Anda Masukkan Salah ";
}else
{
for(int i=0; i<jml_index; i++)
{
cout<<"Inputkan Data Ke- "<<(i+1)<<" = ";
cin>>data[i];
data2[i]=data[i];
}
}
}
void seq_search(int cari)
{
int posisi=0, ketemu=0;
for(int i=0;i<jml_index;i++)
{
if(data[i]==cari)
{
ketemu=1;
posisi=i;
break;
}
}
if (ketemu ==1)
{cout<<"Data "<<cari<<" ada di posisi indeks ke- : "<<posisi;}
else{
cout<<"Data "<<cari<<" tak ditemukan ";}
}
void bin_search(int cari)
{
cout<<endl;
SelectionSortASC();
int ketemu=0, kiri=0, kanan=(jml_index-1), tengah;
while (kiri<=kanan && ketemu ==0)
{
tengah =(kiri+kanan)/2;
if(data[tengah]==cari)
ketemu=1;
else if (cari<data[tengah])
kanan = tengah-1;
else
kiri = tengah+1;
}
if(ketemu==1)
{cout<<"Data "<<cari<<" ada di posisi indeks ke- : "<<tengah;}
else{
cout<<"Data "<<cari<<" tak ditemukan ";}
}
main()
{
int pil,cari;
clrscr();
do
{
clrscr();
gotoxy(16,2);cprintf("====================================================");
gotoxy(16,3); cprintf(" PROGRAM SEARCHING ");
gotoxy(16,4); cprintf("====================================================");
gotoxy(16,5); cprintf(" ");
gotoxy(16,6); cprintf("====================================================");
cout<<endl<<endl;
gotoxy(16,10);cprintf("=====================[ MENU ]======================");
cout<<endl<<endl;
gotoxy(16,12);cprintf("1. Masukkan Data");endl;
gotoxy(16,13);cprintf("2. Squential Sort");endl;
gotoxy(16,14);cprintf("3. Binary Sort");endl;
gotoxy(16,15);cprintf("4. Keluar");endl;
cout<<endl<<endl<<endl;
cout<<"Masukkan Pilihan : ";
cin>>pil;
switch (pil)
{
case 1: Input();break;
case 2:
cout<<endl;
cout<<"============================"<<endl;
cout<<"Data Sebelumnya "; tampil(data);
cout<<"Masukkan Data Yang Dicari : ";
cin>>cari;
seq_search(cari);break;
case 3:
cout<<endl;
cout<<"============================"<<endl;
cout<<"Data Sebelumnya "; tampil(data2);
cout<<"Masukkan Data yang dicari : ";
cin>>cari;
bin_search(cari
);break;
}
getch();
}while (pil!=4);
}
Comments
Post a Comment