Searching data array with random data.
Sequential Search, Binary Search, Interpolation Search.
source code:
#include<stdio.h>
#include<stdlib.h>
int main(){
int flag=0,count=0,n,i,j,x,temp,ktm=0;
printf("Masukkan banyak data>");
scanf("%d",&n);
int dat[n];
printf("\nGenerating Random Number....\n");
for(i=0;i<n;i++){
dat[i]=rand()%(100)+1;
printf("%d ",dat[i]);
}
printf("\nDone!\n");
////////Sorting dulu gan///////////////
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(dat[i]>dat[j]){
temp=dat[i];
dat[i]=dat[j];
dat[j]=temp;
}
}
}
printf("\nSorting....\n");
for(i=0;i<n;i++){
printf("%d ",dat[i]);
}
printf("\nDone!\n");
////////////////////////////
//////////////////////////
printf("\nMasukkan data yang ingin dicari>");
scanf("%d",&x);
///////////////////////////
////////Sequential search/////////////
printf("\n[ Sequential Search ]\n");
count=0; temp=0;
for(i=0;i<n;i++){
count++;
if(dat[i]==x){
temp=1;
break;
}else
temp=0;
}
if(temp==1) printf("Data ada! Banyak perulangan %d kali\n",count);
else printf("Data tidak ada!\n");
//////////////////////////////////////
////////Binary Search/////////////////
int left=0,mid,right=n-1;
count=0;
printf("\n[ Binary Search ]\n");
while(left<=right && ktm==0){
mid=(left+right)/2;
count++;
printf("Search ke-%d=%d\n",count,dat[mid]);
if(dat[mid]==x) ktm=1;
else if(x<dat[mid]){
right=mid-1;
}
else{
left=mid+1;
}
}
if(ktm==1) printf("Data ada! Banyak perulangan %d kali\n",count);
else printf("Data tidak ada!\n");
//////////////////////////////////////
///////Interpolation Serach///////////
printf("\n[ Interpolation Search ]\n");
int low=0,high=n-1,pos,tm=0;
count=0;
float pos1;
do{
count++;
printf("Search ke-%d=%d\n",count,dat[pos+1]);
pos=((x-dat[low])/(dat[high]-dat[low]))*(high-low)+low;
if(dat[pos] == x){
tm++;
break;
}
if (dat[pos] > x)
high = pos-1;
else if (dat[pos] < x)
low = pos + 1;
}
while(x >= dat[low] && x <= dat[high]);
if (tm>0){
printf("Data ada! Banyak perulangan %d kali\n",count);
}else{
printf("Data tidak ada!\n");
}
//////////////////////////////
}
Sequential Search, Binary Search, Interpolation Search.
source code:
#include<stdio.h>
#include<stdlib.h>
int main(){
int flag=0,count=0,n,i,j,x,temp,ktm=0;
printf("Masukkan banyak data>");
scanf("%d",&n);
int dat[n];
printf("\nGenerating Random Number....\n");
for(i=0;i<n;i++){
dat[i]=rand()%(100)+1;
printf("%d ",dat[i]);
}
printf("\nDone!\n");
////////Sorting dulu gan///////////////
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(dat[i]>dat[j]){
temp=dat[i];
dat[i]=dat[j];
dat[j]=temp;
}
}
}
printf("\nSorting....\n");
for(i=0;i<n;i++){
printf("%d ",dat[i]);
}
printf("\nDone!\n");
////////////////////////////
//////////////////////////
printf("\nMasukkan data yang ingin dicari>");
scanf("%d",&x);
///////////////////////////
////////Sequential search/////////////
printf("\n[ Sequential Search ]\n");
count=0; temp=0;
for(i=0;i<n;i++){
count++;
if(dat[i]==x){
temp=1;
break;
}else
temp=0;
}
if(temp==1) printf("Data ada! Banyak perulangan %d kali\n",count);
else printf("Data tidak ada!\n");
//////////////////////////////////////
////////Binary Search/////////////////
int left=0,mid,right=n-1;
count=0;
printf("\n[ Binary Search ]\n");
while(left<=right && ktm==0){
mid=(left+right)/2;
count++;
printf("Search ke-%d=%d\n",count,dat[mid]);
if(dat[mid]==x) ktm=1;
else if(x<dat[mid]){
right=mid-1;
}
else{
left=mid+1;
}
}
if(ktm==1) printf("Data ada! Banyak perulangan %d kali\n",count);
else printf("Data tidak ada!\n");
//////////////////////////////////////
///////Interpolation Serach///////////
printf("\n[ Interpolation Search ]\n");
int low=0,high=n-1,pos,tm=0;
count=0;
float pos1;
do{
count++;
printf("Search ke-%d=%d\n",count,dat[pos+1]);
pos=((x-dat[low])/(dat[high]-dat[low]))*(high-low)+low;
if(dat[pos] == x){
tm++;
break;
}
if (dat[pos] > x)
high = pos-1;
else if (dat[pos] < x)
low = pos + 1;
}
while(x >= dat[low] && x <= dat[high]);
if (tm>0){
printf("Data ada! Banyak perulangan %d kali\n",count);
}else{
printf("Data tidak ada!\n");
}
//////////////////////////////
}