0% found this document useful (0 votes)
30 views

Code 2

The document discusses implementing a binary search algorithm to search for a target value within a sorted array, explaining that it works by comparing the target to the middle element of the array and recursively searching either the lower or upper half depending on if the target is less than or greater than that middle element, and includes C source code implementing binary search along with sample test cases and outputs.

Uploaded by

Big Buddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Code 2

The document discusses implementing a binary search algorithm to search for a target value within a sorted array, explaining that it works by comparing the target to the middle element of the array and recursively searching either the lower or upper half depending on if the target is less than or greater than that middle element, and includes C source code implementing binary search along with sample test cases and outputs.

Uploaded by

Big Buddy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

S.No: 2 Exp.

Name: Write the code to implement Binary Search Date: 2022-09-10

ID: 210310571886
    Page No:
           
Aim:
Aim: Write a program to implement Binary Search

Theory: In computer science, binary search, also known as half-interval search, logarithmic search, or binary
chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search
compares the target value to the middle element of the array.

Algorithm:

1. Find the midpoint of the array; this will be the element at arr[size/2]. The midpoint divides the array into
two smaller arrays: the lower half of the array consisting of elements 0 to midpoint - 1, and the upper half
of the array consisting of elements midpoint to size - 1.

2. Compare key to arr[midpoint] by calling the user function cmp_proc.


3. If the key is a match, return arr[midpoint]; otherwise


4. If the array consists of only one element return NULL, indicating that there is no match; otherwise

5. If the key is less than the value extracted from arr[midpoint] search the lower half of the array by
recursively calling search; otherwise

6. Search the upper half of the array by recursively calling search.


Source Code:

binarySearch.c

#include<stdio.h>

int binarysearch(int ar[] , int data, int len)

int start=0;

while(start<=len)

int mid=(start+len)/2;

if(ar[mid]==data)

return mid+1;

    ITS Engineering College      2021-2025-CSE_3_B1

else if(ar[mid]>data)

len= mid-1;

else{

start= mid+1;

return -1;

int main()

printf("Enter the total number of elements: ");

int n;

scanf("%d",&n);

ID: 210310571886
    Page No:
           
printf("Enter the element in sorted form: ");

int ar[n];

for(int i=0; i<n; i++)

scanf("%d",&ar[i]);

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

int key;

scanf("%d",&key);

int loc;

loc = binarysearch(ar, key, n-1);

if(loc!=-1)

printf("The number %d is found in the list at the position %d\n",key,loc);

else{

printf("Element not found\n");

return 0;

Execution Results - All test cases have succeeded!

Test Case - 1

User Output
Enter the total number of elements: 7
Enter the element in sorted form: 11 22 33 44 55 66 77
Enter the element which you want to search: 55
The number 55 is found in the list at the position 5

Test Case - 2

User Output
Enter the total number of elements: 5
Enter the element in sorted form: 56 98 118 245 3337
Enter the element which you want to search: 69
    ITS Engineering College      2021-2025-CSE_3_B1

Element not found

You might also like