On Binary Search2 Humza
On Binary Search2 Humza
1
Binary Search
2
How Binary Search Woks ?
1. Initialize three pointers: Mid, Low,
High.
2. Compare the target value to the
Middle element of the array.
3. If they are equal the search is
complete.
4. If the target is less than the middle
element, repeat the search in left
half.
5. If the target is greater, repeat on the
right half.
3
Binary search example
4
Unsuccessful
binary
search
example
5
Advantages and Disadvantages
Advantages of Binary Search
Efficiency: Binary search has a time
complexity of O(log n), making it significantly
faster than linear search, especially for large
datasets.
Wide Applicability: It can be used for
various search problems, including
searching in sorted arrays, sorted linked
lists, and sorted files.
Disadvantages of Binary Search
Requires Sorted Data: must be sorted in
ascending or descending order. Sorting itself
can be time-consuming, especially for large
datasets.