#include <stdio.h>
#include <stdlib.h>
int main()
{
int *A;
int i,n,search;
printf("Enter the size of array\n");
scanf("%d",&n);
A = (int*) malloc(sizeof(int)*n);
printf("Enter the elements in the array\n");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("your elements are\n");
for(i=0;i<n;i++)
printf("%d ",A[i]);
printf("\nEnter the value to search\n");
scanf("%d",&search);
binsearch(A,n,search);
free(A);
}
void binsearch(int *A,int n,int search)
{
int i=0,j=n-1,num;
while(i<=j)
{
num =(i+j)/2;
if(A[num]==search)
{
printf("Number is present at position %d\n",num+1);
return;
}
else if(A[num]<search)
i = num+1;
else
j = num-1;
}
printf("Search is unsuccessful because number is not in the list\n ");
}
#include <stdlib.h>
int main()
{
int *A;
int i,n,search;
printf("Enter the size of array\n");
scanf("%d",&n);
A = (int*) malloc(sizeof(int)*n);
printf("Enter the elements in the array\n");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("your elements are\n");
for(i=0;i<n;i++)
printf("%d ",A[i]);
printf("\nEnter the value to search\n");
scanf("%d",&search);
binsearch(A,n,search);
free(A);
}
void binsearch(int *A,int n,int search)
{
int i=0,j=n-1,num;
while(i<=j)
{
num =(i+j)/2;
if(A[num]==search)
{
printf("Number is present at position %d\n",num+1);
return;
}
else if(A[num]<search)
i = num+1;
else
j = num-1;
}
printf("Search is unsuccessful because number is not in the list\n ");
}
0 comments:
Post a Comment