0% found this document useful (0 votes)
15 views14 pages

Group 2 Binary Search

Uploaded by

wwwpts.1
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)
15 views14 pages

Group 2 Binary Search

Uploaded by

wwwpts.1
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/ 14

GROUP 2

BINARY SEARCH
MUNASHE
MUNASHE T
T MWOYOWESHUMBA
MWOYOWESHUMBA DEAN
DEAN K
K CHAVUNDUKA R2312968Q
CHAVUNDUKA R2310202R
MUNASHE
MUNASHE T MWOYOWESHUMBA
T MWOYOWESHUMBA DEAN K CHAVUNDUKA R2310202R
R2310202R
R2310202R
R2310202R
R2310202R
LEON
LEON KHOZA R238930X
MATTHEW
MATTHEW
MATTHEW
KASANGO
KASANGO R2310202R LEON KHOZA
KHOZA R2310202R
R2310202R
MATTHEW KASANGO R2310202R
KASANGO R2310202R
R2310288Y
FRANCIS CHIHWARI R234540H
MICHAEL
MICHAEL CHITONGA
CHITONGA R2310202R
MICHAEL
MICHAEL CHITONGA
CHITONGA R2310202R
R2310202R
R238371J
INGA
MUTSAM R2310202R
MUTARE R2310134W
FRANCIS
FRANCIS CHIHWARI
CHIHWARI R2310202R
R2310202R
EDDINGTON
EDDINGTON CHINEMBIRI
CHINEMBIRI R2310202R
EDDINGTON
EDDINGTON CHINEMBIRI R2310202R
CHINEMBIRI R2310202R
R235551F BRANDON LINJE R237816G
01 INTRODUCTION

OVERVIE
02 ALGORITHM STEPS

03 DEMONSTRATION

W
04 IMPLEMENTATION -
PYTHON
05 APPLICATIONS

06 TIME COMPLEXITY
ANALYSIS
07 ADVANTAGES AND
PROS AND CONS
INTRODUCTIO
• Binary Search is an efficient algorithm used to find an
item within a sorted list of items

• It works by repeatedly dividing the list in half,


narrowing down the possible locations for the desired
item until only one location remains.

N
• Binary search is incredibly efficient because it reduces
the search space exponentially with each step. Contrast
01
this with linear search, where you’d need to examine
every item sequentially
• Binary Search is one of the fastest searching
algorithms.
• It is used for finding the location of an element
in a linear array.
• It works on the principle of divide and conquer
• So, the elements must be arranged in:
technique..
• Binary Search Algorithm can be applied only on • Either ascending order if the elements are numbers.
Sorted arrays. • Or dictionary order if the elements are strings.

• To apply binary search on an unsorted array:

• First, sort the array using some sorting technique.


• Then, use binary search algorithm.
02 ALGORITHM STEPS
Here's a step-by-step explanation of how binary searching works:

1. Start with a sorted list of items.

2. Find the middle item of the list.

3. Compare the middle item to the item you're searching for.

4. If the middle item is the item you're searching for, return its location.

5. If the middle item is greater than the item you're searching for, repeat steps 2-4 with the left

half of the list.

6. If the middle item is less than the item you're searching for, repeat steps 2-4 with the right

half of the list.

7. Repeat steps 2-6 until the item is found/list is empty


02.2 PSEUDOCODE
Start

Let A be a sorted array.


2. Let n be the size of the array.
3. Let x be the value to be searched.
4. Set lowerBound = 0 (the index of the first element).
5. Set upperBound = n - 1 (the index of the last element).
6. While lowerBound <= upperBound:
i) set midpoint = lowerBound +upperBound / 2 (calculate the index of the middle element).

ii) if A[midpoint] = x: - Exit the loop: “x is found at location midpoint”.

iii) if A[midpoint] < x: - Set lowerBound = midpoint + 1 (discard the left half of the array).

iv) If A[midpoint] is greater than x: - Set upperBound = midpoint - 1 (discard the right half of the
array).
else
7. Exit the loop: “x does not exist in the array”.

End
ALTERNATIVELY
Begin
Set beg = 0
Consider-
Set end = n-1
• There is a linear array ‘a’ of size ‘n’.
Set mid = (beg + end) / 2
• Binary search algorithm is being used to search
while ( (beg <= end) and (a[mid] ≠ item) ) do
an element ‘item’ in this linear array.
if (item < a[mid]) then
• If search ends in success, it sets loc to the index
Set end = mid - 1
of the element otherwise it sets loc to -1.
else
• Variables beg and end keeps track of the index of
Set beg = mid + 1
the first and last element of the array or sub array
endif
in which the element is being searched at that
Set mid = (beg + end) / 2
instant.
endwhile
• Variable mid keeps track of the index of the
if (beg > end) then
middle element of that array or sub array in which
Set loc = -1
the element is being searched at that instant.
else
Set loc = mid
endif
End
03 DEMONSTRATION
We change our low to mid + 1 and find the new mid value again.
For a binary search to work, it is mandatory for the target array low = mid + 1
to be sorted. We shall learn the process of binary search with a
mid = (low + high) / 2
pictorial example. The following is our sorted array and let us
assume that we need to search the location of value 31 using Our new mid is 7 now. We compare the value stored at location 7 with
binary search. our target value 31.

First, we shall determine half of the array by using this


The value stored at location 7 is not a match, rather it is greater than
formula − mid = (low + high) / 2 what we are looking for. So, the value must be in the lower part from this
Here it is, (0 + 9) / 2 = 4.5 but we’ll take 4 as the mid location.
of the array since we use only integers in determining
the index.

Hence, we calculate the mid again. This time it is 5.

Now we compare the value stored at location 4, with the value


being searched, i.e. 31. We find that the value at location 4 is 27,
which is not a match. As the Target value is greater than 27 and We compare the value stored at location 5 with our target value. We
we have a sorted array, so we know that the target value must be find that it is a match.
in the upper portion of the array.

We conclude that the target value 31 is stored at location 5.


04 IMPLEMENTATION - PYTHON
RECURSIVE METHOD
CAN BE DONE IN TWO
WAYS :
ITERATIVE METHOD
05 APPLICATIONS

• Applications of binary search algorithms

• Binary search is a very efficient algorithm for searching a sorted array. It is often used in applications where speed is important, such as:

• Database searching: Binary search can be used to quickly find a record in a database table. For example, a bank might use binary search to quickly
find a customer's account information.
• File searching: Binary search can be used to quickly find a file in a directory. For example, a music player might use binary search to quickly find a
song file. A typical music library may contain thousands or even millions of songs, and binary search can quickly find the desired song in
logarithmic time.
• Spell checking: Binary search can be used to quickly check if a word is in a dictionary. For example, a word processor might use binary search to
quickly check if a word is spelled correctly
• Sorting algorithms: Binary search is used in some sorting algorithms, such as merge sort and quick sort, to quickly find the correct position for an
element in the sorted array.
• Machine learning: Binary search can be used to quickly find the best hyperparameters for a machine learning model. For example, a machine
learning engineer might use binary search to quickly find the best learning rate for a neural network.
• Video games: Binary search can be used to quickly find the nearest object to the player in a video game. For example, a racing game might use
binary search to quickly find the nearest checkpoint to the player's car.
Here are some specific examples of how binary search is used in real- Situations Ideal for Binary Search
world applications:
Sorted Data: The fundamental requirement for binary search is that
• Google: Google uses binary search to quickly find web pages that the data must be sorted in either ascending or descending order.
are relevant to a user's search query. Frequent Searches: If you need to repeatedly search for elements
• Amazon: Amazon uses binary search to quickly find products within a large dataset, binary search's efficiency will provide
that match a customer's search criteria. significant performance benefits compared to a linear search.
• Facebook: Facebook uses binary search to quickly find friends Random Access: Binary search works best with data structures that
and other users on the platform. When you search for a friend or allow random access, meaning you can directly jump to the middle
other user on Facebook, Facebook uses binary search to quickly element.
find the person you are looking for and display their profile to Real-World Examples
you. Searching Dictionaries: Imagine the words in a physical dictionary.
Finding a word using a binary search approach is much faster than
flipping through page by page.
Debugging: When a bug pops up in a large codebase, developers can
apply a binary search-like approach by isolating the problematic code
section more quickly.
06 TIME COMPLEXITY
ANALYSIS
HOW TO DERIVE FORMULA

If we have

Iteration 1 = n/2 Iteration 2= (n/2)/2 = n/2^2

Iteration 3= (n/2^2)/2 = n/2^2 Binary Search time complexity analysis is done below-

Iteration ...
K=
• In each iteration or in each recursive call, the search gets reduced to
1= half of the array.
• So for n elements in the array, there are iterations or recursive calls.
n=
Thus, we have:
=k
Time Complexity of Binary Search Algorithm is O().
K= Here, n is the number of elements in the sorted linear array.

Time complexity = O()


07 ADVANTAGES AND DISADVANTAGES

ADVANTAGES DISADVANTAGES

• It eliminates half of the list from further searching by using the result of • It employs recursive approach which requires more
each comparison. stack space.
• It indicates whether the element being searched is before or after the • Complexity: Binary search is more complex to
current position in the list. implement than linear search.
• This information is used to narrow the search. • Single-Dimensional Arrays only; It works only with
• For large lists of data, it works significantly better than linear search. single-dimensional arrays .
• Sorted data :Binary search method is only implemented on sorted data • Not Versatile: It is less versatile than linear search
that is in an ordered manner because it relies on sorted data.
• memory efficient: it increases the space complexity since it eliminates
half the search space making it use less space and increase in space
complexity
THANK YOU QUESTIONS ?

You might also like