0% found this document useful (0 votes)
16 views4 pages

On Binary Search

Binary search

Uploaded by

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

On Binary Search

Binary search

Uploaded by

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

PPT on Binary Search

By Farhan Akhtar

Imagine you have a dictionary, and you want to find the word 'apple'."

"A dictionary is a big, sorted book of words. The words are arranged
alphabetically from A to Z."

"If you didn’t know about Binary Search, you might start from the first page and
go one by one, looking at each word. But this would take a lot of time if the word
you’re looking for is near the end."

"Luckily, there’s a much faster way to search. This method is called Binary
Search."

What is Binary Search?

In Binary Search, instead of going page by page, we first look at the middle page
of the dictionary. If the word we’re looking for is on the middle page, we’ve found
it! If the word we’re looking for comes before the middle page (alphabetically),
we ignore the second half of the dictionary. If the word we’re looking for comes
after the middle page, we ignore the first half of the dictionary. By ignoring half of
the dictionary in each step, we make the search much faster.

Let’s Understand Binary Search with an example-:


Let’s pretend we’re looking for the word 'apple' in the dictionary.

Step 1: We open the dictionary to the middle page. Let’s say the middle page has
the word 'orange'.

Step 2: Since 'apple' comes before 'orange' alphabetically, we can ignore the
entire second half of the dictionary. Now, we only need to search the first half.

Step 3: We open the middle of this first half. Let’s say we find the word 'banana'
on that page.

Step 4: Since 'apple' comes before 'banana' as well, we now ignore the second
half of this smaller section. We only search the first part.
Step 5: We open the middle of this even smaller section, and we find the word
'apple'!

We've found it by cutting the dictionary in half several times instead of checking
each page one by one.

Note-:A prerequisite thing for Binary Search is we need a Sorted array(It can be
ascending or descending).

Now let’s understand the Working of Binary Search-

2 4 6 8 10 12 14 Suppose we have a
sorted array and we have
to find our Key=10.

Now as after the dictionary example I tell you how this algorithm of searching
work- Here the starting index of this array is 0 and ending index is 6(array.length-1).
Start=0
End=6
Now we will find middle of the array which is called mid index -
The formula of finding int mid = low + (high - low) / 2; (high - low ensures the difference
between the two indices is calculated first, avoiding overflow. Adding low back adjusts the
midpoint correctly.)

2 4 6 8 10 12 14

^ ^ ^
Start mid End

Step1- We will compare our mid to our key if array[mid]==key then we will return the mid
index otherwise we will update the array in which we have to find our key.

Step2-Now after finding the mid we will compare it to our key if our mid element is bigger
than the key then we will find our key in the first part of the array which is left part. So
here our End index get updated like End=mid-1.
2 4 6

^ ^ ^
Start mid End

Step3-Likewise after finding the mid we will compare it to our key if our mid element is
smaller than the key then we will find our key in the second part of the array which is
right part. So here our Start index get updated like Start=mid+1.
10 12 14

^ ^ ^
Start mid End

We are finding the key=10


We will run a while loop until out start index<=End Index.
We will find the mid of the array which will be 8 and 8 != 10 so we will update our array
By following step 2 and 3. As 8<10 so we will search in right part of the array and update
our Start index which will become start=mid+1.
Then after that our array will become 10 12 14 in which 12 will be the mid point and
12!=10 and 12>10 so we will find our element in left part of the array which is only 10 and
out start and end index only point a single element which is 10 now array[mid] will
become ==key(10) we will return mid index.

Pseudo Code-

Start=0, end=n-1;

While(start<=end)
Find mid

Compare mid and key

(mid==key) FOUND

(mid>key) LEFT

(mid<key) RIGHT
Time complexity Analysis—
At every iteration from begin our array size is becoming n/2^k times less as every time we
are dividing our array into half. Our array will be divided until it becomes a unit array.

n/2^k=1
n=2^k

So, mathematically our Time Complexity will become O(log n).

How it is more efficient than linear search?


If we have 1 million size of array then in linear search we have to make 1 million
comparison and on the other hand in Binary Search we have to make 20 comparison only.

You might also like