#include<stdio.h>
#define MAX 5
int a[MAX];
insert()
{
int i;
for(i=0;i<=MAX;i++)
{
printf("\nEnter No :: ");
scanf("%d",&a[i]);
}
}
sort()
{
int i,j,temp;
for(i=0;i<=MAX;i++)
{
for(j=i;j<=MAX;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
}
display()
{
int i;
for(i=0;i<=MAX;i++)
{
printf("\t%d",a[i]);
}
}
binarySearch(int low,int high,int value)
{
int mid;
mid=(low+high)/2;
if((mid==0 && (a[mid]!=value)) || (mid==MAX && (a[mid]!=value)))
{
printf("\nRecord Not Found");
}
else if(a[mid]==value)
{
printf("record found");
}else{
if(value<a[mid])
{
binarySearch(0,mid,value);
}else{
binarySearch(mid,high,value);
}
}
}
int main()
{
int ch,f=-1;
while(f<0)
{
printf("\n1. Insert Values\n2. Display Values\n3. Sort & Display\n4. Binary Search\n5. Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();break;
case 2:
display();break;
case 3:
sort();display();break;
case 4:
printf("Enter Element To search::");
scanf("%d",&ch);
sort();binarySearch(0,MAX+1,ch);break;
case 5: return;
default:
printf("\nInvalid Input");
}
}
}
#define MAX 5
int a[MAX];
insert()
{
int i;
for(i=0;i<=MAX;i++)
{
printf("\nEnter No :: ");
scanf("%d",&a[i]);
}
}
sort()
{
int i,j,temp;
for(i=0;i<=MAX;i++)
{
for(j=i;j<=MAX;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
}
display()
{
int i;
for(i=0;i<=MAX;i++)
{
printf("\t%d",a[i]);
}
}
binarySearch(int low,int high,int value)
{
int mid;
mid=(low+high)/2;
if((mid==0 && (a[mid]!=value)) || (mid==MAX && (a[mid]!=value)))
{
printf("\nRecord Not Found");
}
else if(a[mid]==value)
{
printf("record found");
}else{
if(value<a[mid])
{
binarySearch(0,mid,value);
}else{
binarySearch(mid,high,value);
}
}
}
int main()
{
int ch,f=-1;
while(f<0)
{
printf("\n1. Insert Values\n2. Display Values\n3. Sort & Display\n4. Binary Search\n5. Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();break;
case 2:
display();break;
case 3:
sort();display();break;
case 4:
printf("Enter Element To search::");
scanf("%d",&ch);
sort();binarySearch(0,MAX+1,ch);break;
case 5: return;
default:
printf("\nInvalid Input");
}
}
}
Comments
Post a Comment