Program Searching/Pencarian data menggunakan Binary Search, Squencial Search dan Interpolation Search Mengunakan Bahasa Pemrograman C++
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <cstdlib>
int array[100];
int low, high, pos, cari, flag=0, a, e, i, awal, tengah, akhir, pil, x, d,b,max,kel=1,titip,ok,ed;
float posisi;
void sequential();
void binary();
void interpolation();
main()
{
//memasukkan data
printf("Jumlah Data Yang Anda Inginkan (Max 100): "); scanf("%d", &a);
printf("Masukkan Datanya (tidak boleh sama & harus urut dari terkecil) :\n");
for(i=0;i<a;i++)
{printf("data ke %d = ", i+1);
scanf("%d", &array[i]);}
//menampilkan data
printf("\ndata yang anda masukkan : \n");
for(i=0;i<a;i++)
{printf("%d ", array[i]);}
//pilihan menu
system ("CLS");
while(pil != 4)
{printf("\n\npilihan metode pencarian data: \n");
printf("1. Sequential Search \n");
printf("2. Binary Search \n");
printf("3. Interpolation Search \n");
printf("4. keluar \n");
printf("pilihan anda : ");scanf("%d", &pil);
switch(pil)
{
case 1: //cari data
{system ("CLS");
printf("\t\t\tSequential Search\n");
printf("\nmasukkan data yang dicari : ");
scanf("%d", &cari);
sequential();} break;
case 2: //cari data
{system ("CLS");
printf("\t\t\tBinary Search\n");
printf("\nmasukkan data yang dicari : ");
scanf("%d", &cari);
binary();} break;
case 3: //cari data
{system ("CLS");
printf("\t\t\tInterpolation Search\n");
printf("\nmasukkan data yang dicari : ");
scanf("%d", &cari);
interpolation();} break;
case 4:
system ("CLS");
printf("Copyright (c) http://fachryasprog.blogspot.com");
};
if (pil == 1 || pil == 2 || pil == 3 )
printf("\nTekan enter untuk kembali ke menu utama");
getch();};
return 0;
}
void sequential()
{
for(i=0 ; i<a ; i++)
{if(array[i]==cari)
{flag=1;
break;}
}
if(flag == 1)
{
printf("data yg dicari di index ke-%d", i+1); e = i;
// mengedit data
for(i=e; i<a; i++)
{array[i]= array[i+1];}
printf("\n\nubah datanya = ");
for(i=a;i>e;i--)
{array[i]=array[i-1];}
scanf("%d", &array[e]);
//mencetak dan mengurutkan data
printf("\ndata anda sekarang : \n");
for (kel=0;kel<a;kel++)
{
max=array[kel];
for(i=kel;i<a;i++)
if(array[i]<=max)
{max=array[i];
ok=i;}
titip=array[kel];
array[kel]=array[ok];
array[ok]=titip;
}
for(e=0;e<a;e++)
{printf("%d ",array[e]);}
array[i] == array[e];
}
else printf("data tdk ditemukan");
}
void binary()
{
flag = 0;
awal=0;
akhir=a-1;
while(awal<=akhir && flag == 0)
{
tengah = (awal+akhir)/2;
if(array[tengah]==cari)
{flag=1; break;}
else if(array[tengah]<cari)
{awal=tengah+1;}
else
{akhir=tengah-1;}
}
if(flag==1)
{
printf("data yg dicari di index ke-%d", tengah+1);
// mengedit data
for(i=tengah; i<a; i++)
{array[i]= array[i+1];}
printf("\n\nubah datanya = ");
for(i=a;i>tengah;i--)
{array[i]=array[i-1];}
scanf("%d", &array[tengah]);
//mencetak dan mengurutkan data
printf("\ndata anda sekarang : \n");
for (kel=0;kel<a;kel++)
{
max=array[kel];
for(i=kel;i<a;i++)
if(array[i]<=max)
{max=array[i];
ok=i;}
titip=array[kel];
array[kel]=array[ok];
array[ok]=titip;
}
for(e=0;e<a;e++)
{printf("%d ",array[e]);}
array[i] == array[e];
}
else printf("data tdk ditemukan");
}
void interpolation()
{
flag=0;
low=0;
high=a-1;
do
{
posisi=(float)(cari-array[low])/(array[high]-array[low])*(high-low)+low;
pos=floor(posisi);
if(array[pos]==cari)
{
flag =1;
break;
}
if(array[pos]>cari) high=pos-1;
else if(array[pos]<cari) low=pos+1;
}
while(cari>=array[low] && cari<=array[high]);
if(flag==1)
{
printf("data yg dicari di index ke-%d", pos+1);
// mengedit data
for(i=pos; i<a; i++)
{array[i]= array[i+1];}
printf("\n\nubah datanya = ");
for(i=a;i>pos;i--)
{array[i]=array[i-1];}
scanf("%d", &array[pos]);
//mencetak dan mengurutkan data
printf("\ndata anda sekarang : \n");
for (kel=0;kel<a;kel++)
{
max=array[kel];
for(i=kel;i<a;i++)
if(array[i]<=max)
{max=array[i];
ok=i;}
titip=array[kel];
array[kel]=array[ok];
array[ok]=titip;
}
for(e=0;e<a;e++)
{printf("%d ",array[e]);}
array[i] == array[e];
}
else printf("data tdk ditemukan");
}