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[10],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)
         printf("Number is not found.");
    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.





17 comments:

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...

Idiot he is just trying to show how you can perform a binary search using recursion.
Or you just need a copy paste..

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...

Naveed Cheema 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[20], 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

Sukhpal Singh said...

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

Rohit Shrestha 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 Shrestha said...

its not a binary search....

Rohit Shrestha 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....

Pratyush Sinha 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
printf("Item not found\n");
return 0;
}

Pratyush Sinha 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
printf("not found\n");
return 0;
}

Tarek Magdy said...

μ‘°μΌν˜„ said...

this code has a error

aroosha khj said...

πŸ‘Œ

Michael Albert said...

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