Group 2 Binary Search
Group 2 Binary Search
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
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.
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
6. If the middle item is less than the item you're searching for, repeat steps 2-4 with the right
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.
• 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 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.
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 ?