ilide.info-tg-understanding-culture-society-and-politics-pr_127f984d2904c57ec...jed P
Ad
BINARY SEARCH - A - DATA STRUCTURE AND ALGORITHM.pdf
1. BINARY SEARCH
GROUP 1
EDEN ROSE CONICAN
LESHE SENINA
GREEZE LEE OMANDAM
JHON MARK ALIPOYO
MACGEREM ANRO PAGLINAWAN
2. INTRODUCTION
• Binary search is search algorithm that finds the position of a target value within a
sorted array.
• Binary search is also known as half-interval search or logarithmic search.
• Binary search algorithm works on the principle of Divide and Conquer.
• Binary search can be implemented on only sorted list of elements.
3. Binary search is a searching algorithm for finding an element’s position in a sorted array.
In this approach, the element is always searched in the middle of a portion of an array.
Binary search can be implemented only on a sorted list of items. If the elements are not
sorted already, we need to sort them first.
BINARY SEARCH WORKING
Binary Search Algorithms can be implemented in two ways which are discussed below:
1. Iterative Method
2. Recursive Method
4. 1. ITERATIVE METHOD
• The iterative binary search method is a highly efficient algorithm for finding a specific
target value within a sorted array. It works by repeatedly dividing the search interval in
half, eliminating half of the remaining elements in each steps.
Here how it functions:
1. Initialization
• Start with the entire arrays as the search interval, defining the leftmost index (low) and
rightmost index (high).
• Set the middle index (mid) to the middle of the interval.
5. 2. Iteration:
• Comparison: Compare the target value with the element at the middle index (mid).
- If the target value is equal to the middle element, the search is successful, and the index (mid) is
returned.
- If the target value is less than the middle element, the search continues in the left half of the interval
(low to mid – 1).
- If the target value is greater than the middle element, the search continues in the right half of the
interval (mid + 1 to high).
3. Updating the Search Interval:
• In each iteration, the search interval is halved based on the comparison result.
• If the target value is less than the middle element, the right boundary (high) becomes mid -1.
• If the target value is greater than the middle element, the left boundary (low) becomes mid + 1.
6. 4. Termination:
• The search continues until the left index (low) becomes greater than the right index (high), indicating
that the target value is not present in the array. In this case, the search returns -1.
7. 2. RECURSIVE METHOD
• The recursive binary search algorithms is a powerful and elegant approach to efficiently
finding a target value in a sorted array. It works by repeatedly dividing the search interval
in half, eliminating half of the remaining elements in each steps.
Here how it functions:
1. Base Case:
• If the left index (low) becomes greater than the right index (high), it means that the
target value is not present in the array. In this case, the function returns -1.
8. 2. Recursive Steps:
• Calculate the middle index (mid) of the search interval.
• Compare the target value with the element at the middle index:
- If the target value is equal to the middle element, the search is successful, and the index (mid) is returned.
- If the target value is less than the middle element, recursively search the left half of the array (low to mid -1).
- If the target value is greater than the middle element, recursively search the right half of the array (mid + 1 to
high).
3. Recursion:
• The function calls itself with updated search intervals (low and high) based on the comparison result,
effectively dividing the problem into smaller subproblems.
9. DIVIDE AND CONQUER
• Divide and Conquer is the most well-known algorithm strategy.
• A Divide and Conquer algorithm works by recursively breaking down a problem into
two or more sub-problems of the same or related type, until this become simple enough
to be solved directly.
• The solutions to the sub-problems are then combine to give a solution to the original
problem.
10. EXAMPLE :
• Consider an Array:
A
0 1 2 3 4 5 6 7 8
min mid = ( min + max)/2 max
If(37 == a[mid]) return mid
Else if(37<a[mid]) max=mid-1
Else if(37>a[mid]) min=mid+1
20 35 37 40 45 50 51 55 67
11. ALGORITHM
Input :
A sorted array
N size of array
Key value to be searched
BinarySearch(a,key,n) :
Set min=0
Set max=n-1
while (min < max)
Set mid = (min+max)/2
13. TIME COMPLEXITY
• The running times complexity for Binary Search
are of three types:-
In Worst Case , Time Complexity : O(log n)
In Average Case, Time Complexity : O(log n)
In Best Case , Time Complexity : O(1)
14. ADVANTAGE
• Binary Search is an optimal searching algorithm using
which we can desired element very efficiently.
DISADVANTAGE
Binary Search algoritm requires the list to be sorted.
Then only method is Applicable.
15. APPLICATION
• The Binary Search is an efficient searching method and used to search
record from database.
• For solving non-linear equations with one unknown this method to used.