# Answers for "recursive binary search function"

0

binary search with non recursive function call

``````/* Binary search program in C using both recursive and non recursive functions */

#include <stdio.h>
#define MAX_LEN 10

/* Non-Recursive function*/
void b_search_nonrecursive(int l[],int num,int ele)
{
int l1,i,j, flag = 0;
l1 = 0;
i = num-1;
while(l1 <= i)
{
j = (l1+i)/2;
if( l[j] == ele)
{
printf("\nThe element %d is present at position %d in list\n",ele,j);
flag =1;
break;
}
else
if(l[j] < ele)
l1 = j+1;
else
i = j-1;
}
if( flag == 0)
printf("\nThe element %d is not present in the list\n",ele);
}

/* Recursive function*/
int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a)
{
int m,pos;
if (arrayStart<=arrayEnd)
{
m=(arrayStart+arrayEnd)/2;
if (l[m]==a)
return m;
else if (a<l[m])
return b_search_recursive(l,arrayStart,m-1,a);
else
return b_search_recursive(l,m+1,arrayEnd,a);
}
return -1;
}

{
int i;
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
scanf("%d",&l[i]);
}

void print_list(int l[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\t",l[i]);
}

/*main function*/
main()
{
int l[MAX_LEN], num, ele,f,l1,a;
int ch,pos;

//clrscr();

printf("======================================================");
printf("\n=====================================================");
printf("\n[1] Binary Search using Recursion method");
printf("\n[2] Binary Search using Non-Recursion method");
scanf("%d",&ch);

if(ch<=2 & ch>0)
{
printf("\nEnter the number of elements : ");
scanf("%d",&num);
printf("\nElements present in the list are:\n\n");
print_list(l,num);
printf("\n\nEnter the element you want to search:\n\n");
scanf("%d",&ele);

switch(ch)
{
case 1:printf("\nRecursive method:\n");
pos=b_search_recursive(l,0,num,ele);
if(pos==-1)
{
}
else
{
printf("Element is found at %d position",pos);
}
//getch();
break;

case 2:printf("\nNon-Recursive method:\n");
b_search_nonrecursive(l,num,ele);
//getch();
break;
}
}
//getch();
}``````
Posted by: Guest on January-21-2021
0

binary search recursive in c++

``````#include <bits/stdc++.h>
using namespace std;

// A recursive binary search function. It returns
// location of x in given array arr[l..r] is present,
// otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;

// If the element is present at the middle
// itself
if (arr[mid] == x)
return mid;

// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);

// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
}

// We reach here when element is not
// present in array
return -1;
}

int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}``````
Posted by: Guest on November-09-2021
1

binary search iterative

``````// Binary Search using Iterative Approach

import java.io.*;
class Binary_Search
{
public static void main(String[] args) throws Exception
{
Binary_Search obj = new Binary_Search();
System.out.println("Insert the length of the Array : ");
int arr[] = new int[n];
System.out.println("Insert elements into the array : ");
for(int i=0;i<n;i++)
{
}
System.out.println("Enter the num which you want to Search : ");
obj.logic(arr,num);
}
void logic(int arr[],int num)
{
int r = arr.length - 1;
int l = 0;
int mid;
while(l<=r)
{
mid = l + (r-l)/2;
if(arr[mid] == num)
{
System.out.println("Number found at "+mid+"th index");
break;
}
else if(arr[mid]>num)
{
r = mid - 1;
}
else
{
l = mid + 1;
}
}
}
}``````
Posted by: Guest on June-23-2020