Binary Search
Binary Search
Done by
K .Ram prasath
Binary
Search
Recursive Binary Search Algorithm: The recursive approach uses the same
principle as the iterative one but divides the problem into smaller sub-
problems using recursive calls.
Conditions for when to apply Binary
Search in a DataStructure:
Binary Search is an efficient algorithm for finding an element in a sorted data
structure. Here are the conditions for when to apply Binary Search:
Sorted Data: The data structure must be sorted in order for Binary Search to work
correctly.
Direct Access: The data structure should allow direct access to any element in
constant time, which is typical of arrays.
Large Data Sets: It is particularly useful for large data sets because it significantly
reduces the number of comparisons needed compared to linear search.
Contiguous Memory: Binary Search is most efficient when the data is stored in
contiguous memory, as in the case of arrays.
Non-complex Structure: The data should not have complex relationships or
structures that would impede the direct access required by Binary Search.
Advantage Disadvantages
Fast search time: Binary search has a time Requires sorted data: The data must be sorted
complexity of O(log n), making it much beforehand, which can be a disadvantage if
faster than linear search for large data sets. sorting has not already been done.
Uses less memory: It doesn’t require Not efficient for small arrays: For small data
additional storage space, as it operates sets, the overhead of the algorithm may not be
directly on the sorted array. worth it, and a linear search could be more
Works on sorted data: It’s specifically practical.
designed for sorted arrays, which is Poor performance on linked lists: Binary
common in many applications. search requires direct access to the middle
Easy to understand: The algorithm is elements, which is not efficient with linked lists.
straightforward, making it simple to learn Repeated elements cause inefficiency: If
and implement. there are many identical elements, binary
Efficient for large data: The larger the data search can perform unnecessary comparisons.
set, the more efficient binary search Not suitable for mutable data: If the data
becomes compared to other search changes frequently, maintaining a sorted order
method. becomes a challenge, making binary search
less suitable.
Application of
Binary search
Searching in Sorted Arrays: The primary use of binary search is to find an element in a sorted
array efficiently.
Finding Square Roots: Binary search can determine if a number is a perfect square by
searching through a range of integer.
Finding the First Occurrence: It can locate the first element in a sorted array that is
greater than or equal to a given value.
Computing Frequency: Binary search helps in finding the frequency of a target value
in a sorted array of integers.
Peak Finding: It’s used to find a peak element in an array where elements first increase
and then decrease.
Rotated Array Search: If a sorted array is rotated, binary search can still find a target
value within it
THANK
YOU