On Binary Search
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."
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.
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).
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
Pseudo Code-
Start=0, end=n-1;
While(start<=end)
Find mid
(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