0% found this document useful (0 votes)
986 views10 pages

Binary Search

Binary search is an algorithm used to search for a target element in a sorted array. It works by comparing the middle element of the array to the target, and recursively searching either the lower or upper half. The time complexity is O(1) in the best case and O(logn) in the worst case, where n is the number of elements. It is useful for quickly searching sorted data like books in a library.

Uploaded by

vyshnavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
986 views10 pages

Binary Search

Binary search is an algorithm used to search for a target element in a sorted array. It works by comparing the middle element of the array to the target, and recursively searching either the lower or upper half. The time complexity is O(1) in the best case and O(logn) in the worst case, where n is the number of elements. It is useful for quickly searching sorted data like books in a library.

Uploaded by

vyshnavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

BINARY SEARCH

Binary search is a searching algorithm which


is used to search or find a particular element
from multiple elements which are stored in an
array.

Always the array to which we apply the


concept of binary search has to be sorted.

In binary search always the search begins


with comparing middle element of the array
with the target element.
BINARY SEARCH
In binary search always the search begins
with comparing middle element of the array
with the target element.

It works on the principle of divide and


conquer.

It is also known as half interval search,


logarithmic search,or binary chop.
IMPLEMENTATION OF
BINARY SEARCH

STEP 1:  Read the search element from the user.


STEP 2: Find the middle element in the sorted list.
STEP 3: Compare the search element with the middle element in
the sorted list.

 X==A[MID]
MID POINT=  X<A[MID]
Start+end
2  X>A[MID]
ALGORITHM FOR BINARY SEARCH

STEP 1:      key == array[middle]


the search is successful, return middle.

STEP 2:     key < array[middle]


binary search the current lower half of
the array.

STEP 3:      key > array[middle]


binary search the current upper half of
the array.
}
the search is not successful; return –1
#include<stdio.h>
#include<conio.h>

void main()
{
int first, last, middle, size, i, sElement, list[100];
clrscr();

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


scanf("%d",&size);

printf("Enter %d integer values in Assending order\n",


size);

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


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

printf("Enter value to be search: ");


scanf("%d", &sElement);
first = 0;
last = size - 1;
middle = (first+last)/2;
while (first <= last)
{
if (list[middle] < sElement)
first = middle + 1;
else if (list[middle] == sElement)
{
printf("Element found at index %d.\n",middle);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Element Not found in the list.");
getch();
}
OUTPUT:
enter 5 integer values in ascending order

13579

Enter value to be search: 1

Element found at index 1.


EXAMPLE:

2 3 5 8 9 10 11 14 18 20
0 1 2 3 4 5 6 7 8 9

VALUE TO BE SEARCHED IS : 18

ELEMENT IS FOUND AT INDEX 8


TIME COMPLEXITY :

In the best case the time


complexity of binary search is O(1).

In the worst case the time


complexity of binary search is
O(logn).

Here n= Number of elements in an


array.
APPLICATIONS OF BINARY SEARCH

Searching for a book in the library, where the books are


arranged in alphabetical order in racks.

 Our target element is the book we prefer to read. Instead of


hopelessly running around in circles and ruffling through every
rack to find our book we can apply this algorithm to find that
book without breaking a sweat. All we need to do is determine
the total number of racks, then find the middle rack.

 Algorithms are not only needed by our computers but is


indeed well knitted in various aspects of our daily life.
Algorithms serve as recipes to a more convenient life.

You might also like