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

Program To Implement Binary Search

The document describes a program to implement binary search on a sorted array. It begins with an explanation of binary search, then shows the C code to implement it. It takes an array as input, finds the middle index repeatedly, and checks if the target is equal to, less than, or greater than the element at the middle to narrow the search space until the target is found or not. It provides a demonstration on a sample sorted array to search for the element 7.

Uploaded by

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

Program To Implement Binary Search

The document describes a program to implement binary search on a sorted array. It begins with an explanation of binary search, then shows the C code to implement it. It takes an array as input, finds the middle index repeatedly, and checks if the target is equal to, less than, or greater than the element at the middle to narrow the search space until the target is found or not. It provides a demonstration on a sample sorted array to search for the element 7.

Uploaded by

subh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

PROGRAM TO

IMPLEMENT
BINARY SEARCH
BY:
SHIPRANJALI
BCA-I(E)
WHAT IS BINARY SEARCH ?

In computer science binary search is a search


algorithm that finds the position of a target value
within a sorted array.
PROGRAM TO IMPLEMENT BINARY
SEARCH
#include<stdio.h>

#include<conio.h>

void main()

int ar[120],item,i,s, loc=0,B,E,M;

clrscr();

printf("Enter the size of the array:");

scanf("%d",&s);

printf("\nEnter %d elements:",s);

for(i=0;i<s;i++)

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

}
printf("\nEnter the element to be search:");
scanf("%d",&item);
B=0;
E=s-1;
M=(B+E)/2;
while(B<=E)
{ if(ar[M]==item)
{ loc=M+1;
break;
}
else if(ar[M]<item)
B=M+1;
else
E=M-1;
M=(E+B)/2;
}
if(ar[M]==item)
printf(“\nElement %d is found and at location %d.",item,loc);
else
printf(“\nElement not found");
getch();
}
DEMONSTRATION:

Suppose we have taken a sorted array of size 7.


Let the element for which we are searching be 7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53
[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

B M E
B=0, E=s-1 and M=(E+B)/2
Is B<=E ? YES.
[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is ar[M]=7 ? NO.
[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is ar[M]<7 ? NO. This implies E=M-1.


[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the item in this area. {M=(E+B)/2}


[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

B M E Is B<=E ? YES.
[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is ar[M]=7 ? NO.
[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is ar[M]<7 ? YES. This implies B=M+1.


[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the item in this area. {M=(E+B)/2}


[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

(B,M,E) Is B<=E ? YES.


[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is ar[M]=7 ? YES.
Hence the item is found,
Searching complete.
OUTPUT:

You might also like