## INDEX

### C program for binary search using recursion

Write a simple code for binary search using function recursion in c programming language

#include<stdio.h>
int main(){

int a,i,n,m,c,l,u;

printf("Enter the size of an array: ");
scanf("%d",&n);

printf("Enter the elements of the array: " );
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}

printf("Enter the number to be search: ");
scanf("%d",&m);

l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
else
printf("Number is found.");

return 0;
}

int binary(int a[],int n,int m,int l,int u){

int mid,c=0;

if(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
}
else if(m<a[mid]){
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}

Sample output:
Enter the size of an array: 5
Enter the elements of the array: 8 9 10 11 12
Enter the number to be search: 8
Number is found. Anonymous said...

there are 1 error
pls check it at first Anonymous said...

please provide prototype for the binary function before declaring main:

int binary(int[],int,int,int,int); Anonymous said...

it is waste pass "n" (size of the array) as argument at calling binary() in main and also in recursive calls.........
just check it...

Unknown said...

#include //In easy way Binary search and replacement through recursion Cheema's idea
int new_array(int *a , int j ,int n);
int binary(int *a, int i, int search ,int temp);
int main()
{int i , a, search , temp , n, j=1;
printf("how long u want an array\n ");
scanf("%d",&n);
printf("\nEnter array elements:\n");
for(i=1;i<=n;i++)
{ scanf("%d",&a[i]); }
printf("Enter value for search");
scanf("%d",&search);
printf("Enter new element for replacement at ur search\n");
scanf("%d",&temp);

binary(a , i, search, temp);
new_array(a , j, n);
getch();
}
int binary(int *a, int i, int search, int temp)
{
if(i==0) { return 0; }
if(a[i]==search)
{printf("\nfound at location %d\n",i);
a[i]=temp;
printf("\nNew element added to array at location %d and its value is %d",i, a[i]);
}
i--;
binary(a , i ,search, temp);
}
int new_array(int *a , int j ,int n)
{ if(j==n+1) { return; }
printf("\nNew array is %d \n",a[j]);

j++;
new_array(a , j, n); } Anonymous said...

yeah mothafocka..lets play sm shit ove here.. fuck u said...

i am my cocaine and i was in the dork knoight

Unknown said...

thanks alot for Naveed Cheema for correction in code...as it help me in understanding that code easily...

Rohit said...

if(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
}
else if(m<a[mid]){
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
} //error
else //error
return c;
it will search if and only if the given input are in ascending order

Rohit said...

its not a binary search....

Rohit said...

its just a simple search from last list(number) to first list(number).... for binary search the list or array should be in ascending order and we will search the number from mid term....

Unknown said...

#include

/*binary search without using recursion*/

int main(){
int arr[] = {10, 20, 30, 40, 50, 60, 70};
int search;
int low, mid, high;
int found = 0;
printf("Enter the item to be search: ");
scanf("%d", &search);
low = 0;
high = (sizeof(arr) / sizeof(int)) - 1;
while (low <= high){
mid = (low + high) / 2;
if (arr[mid] == search){
found = 1;
break;
}
else if (arr[mid] > search)
high = mid - 1;
else if (arr[mid] < search)
low = mid + 1;
}
if (found == 1)
printf("Item found\n");
else
return 0;
}

Unknown said...

#include

/*binary search using recursion*/
int binary_search(int ptr[], int item, int low, int high){
int mid = 0;
if (low <= high){
mid = (low + high) / 2;
if (ptr[mid] == item)
return 1;
else if (low == high)
return 0;
else if (ptr[mid] < item)
binary_search(ptr, item, mid + 1, high);
else if (ptr[mid] > item)
binary_search(ptr, item, low, mid - 1);
}
else
return 0;
}

int main(){
int arr[] = { 10, 20, 30, 40, 50, 60, 70 };
int search, found=0;
printf("Enter the number to be searched: ");
scanf("%d",&search);
found = binary_search(arr, search, 0, 6);
if (found == 1)
printf("found\n");
else
return 0;
}

Unknown said...

Veritas said...

this code has a error Anonymous said...

👌

Unknown said...

Just copy the function part and put it above the main(), it should work..