Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Goeduhub's Online Courses @ Udemy in Just INR 360/-
Online Training - Youtube Live Class Link
0 like 0 dislike
655 views
in Anna University B.Tech (IT-III Sem) Programming And Data Structure Lab by Goeduhub's Expert (5.8k points)

C program that use both recursive and non recursive functions to perform the Binary search

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

1 Answer

0 like 0 dislike
by Goeduhub's Expert (5.8k points)
edited by
 
Best answer

Binary search: It is a sorting algorithm which follows divide and conquer algorithm in which, the list is divided into two parts and then each element is compared to  middle element of the list. If we get a match then, position of middle element is returned otherwise, we search into either of the halves depending upon the result produced through the match.

Example

 Find 103 in {1, 5, 6, 18, 19, 25, 46, 78, 102, 114}. 

  • Step 1 - middle element is 19 < 103 : 1, 5 ,6 ,18 ,19 ,25, 46 ,78, 102 ,114
  • Step 2 - middle element is 78 < 103 : 1 ,5 ,6, 18 ,19 ,25, 46 ,78, 102 ,114 
  • Step 3 - middle element is 102 < 103 : 1, 5, 6, 18, 19 ,25, 46, 78, 102, 114
  • Step 4 - middle element is 114 > 103 : 1 ,5 ,6, 18, 19, 25, 46 ,78 ,102, 114 
  • Step 5 - searched value is absent : 1 , 5 , 6 ,18 ,19 , 25 , 46 , 78 , 102 , 114

Algorithm

BINARY_SEARCH(A, lower_bound, upper_bound, VAL)

  1. [INITIALIZE] SET BEG = lower_bound
    END = upper_bound, POS = - 1
  2. Repeat Steps 3 and 4 while BEG <=END
  3. SET MID = (BEG + END)/2
  4. IF A[MID] = VAL
    SET POS = MID
    PRINT POS
    Go to Step 6
    ELSE IF A[MID] > VAL
    SET END = MID - 1
    ELSE
    SET BEG = MID + 1
    [END OF IF]
    [END OF LOOP]
  5. IF POS = -1
    PRINT "VALUE IS NOT PRESENT IN THE ARRAY"
    [END OF IF]
  6. EXIT

Program Code

#include<stdio.h>

#include<conio.h>

int binarySearch(int[], int, int, int);

int binSearch(int[], int, int, int);

void main ()

{

    clrscr();

    int arr[10] = {16, 19, 20, 23, 45, 56, 78, 90, 96, 100};

    int item, location=-1,location1=-1;

    printf("Enter the item which you want to search: ");

    scanf("%d",&item);

    location = binarySearch(arr, 0, 9, item);

    location1 = binSearch(arr, 0, 9, item);

    printf("Using Recursive function!\n");

    if(location != -1)

    {

printf("Item found at location: %d",location);

    }

    else

    {

printf("Item not found");

    }

    printf("\n\nUsing Non-Recursive function!\n");

    if(location1 != -1)

    {

printf("Item found at location: %d",location);

    }

    else

    {

printf("Item not found");

    }

    getch();

}

int binarySearch(int a[], int beg, int end, int item)

{

    int mid;

    if(end >= beg)

    {

mid = (beg + end)/2;

if(a[mid] == item)

{

    return mid+1;

}

else if(a[mid] < item)

{

    return binarySearch(a,mid+1,end,item);

}

else

        {  

            return binarySearch(a,beg,mid-1,item);  

        }  

    }  

    return -1;   

}  

int binSearch(int a[], int beg, int end, int item)  

{  

    int mid;  

    while(end >= beg)   

    {     

        mid = (beg + end)/2;  

        if(a[mid] == item)  

        {  

            return mid+1;  

        }  

        else if(a[mid] < item)   

        {  

            beg = mid + 1;  

        }  

        else   

        {  

            end = mid - 1;   

        }    

    }  

    return -1;   

}   

Output


For more Anna University III Sem DSA Lab Experiments Click here

For other University DSA Lab Experiments Click here


    Ezoicreport this ad

    3.3k questions

    7.1k answers

    390 comments

    4.4k users

    Related questions

    0 like 0 dislike
    1 answer 698 views
    Ezoicreport this ad

     Goeduhub:

    About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
    ...