KEMBAR78
an Introduction to binary search algorithm | PPTX
Binary Search
Efficient Searching Algorithm
Introduction
• Binary search is a divide-and-conquer algorithm used for searching in
a sorted array.
• It efficiently finds the position of a target value within the array.
How Binary Search Works
• Binary search starts by comparing the target value with the middle
element of the array.
• If the middle element is the target, the search is successful.
• If the target is smaller, the search continues on the left half of the
array.
• If the target is larger, the search continues on the right half of the
array.
• This process is repeated until the target is found or the search range
becomes empty.
Algorithm Steps
• Start with a sorted array and a target value.
• Set the search range to the entire array.
• Calculate the middle index of the search range.
• Compare the middle element with the target value.
• If they are equal, the search is successful.
• If the target is smaller, update the search range to the left half of the array
and go to step 3.
• If the target is larger, update the search range to the right half of the array
and go to step 3.
• Repeat steps 3-7 until the target is found or the search range is empty.
Implementation
• It can be implemented using iteration as well as recursion
Algorithm Steps
int binarySearch(int array[], int x, int start, int end) {
// Iterative Binary Search
// Repeat until the pointers start and end meet each other
while (start <= end) {
int mid = (start + start) / 2;
if (array[mid] == x)
return mid;
Algorithm Steps
if (array[mid] < x)
start = mid + 1;
else
end = mid - 1;
}
return -1;
}
Algorithm Steps
// Recursive Binary Search
int binarySearchRecursive(int arr[], int left, int right, int target) {
if (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] > target)
return binarySearchRecursive(arr, left, mid - 1, target);
else
return binarySearchRecursive(arr, mid + 1, right, target);
}
return -1;
}
Time Complexity
• Binary search has a time complexity of O(log n), where n is the
number of elements in the array.
• It reduces the search space by half in each iteration, leading to a
logarithmic time complexity.
Advantages of Binary Search
• Efficient searching algorithm for sorted arrays.
• Works well even with large datasets.
• Requires fewer comparisons compared to linear search.
Limitations of Binary Search
• Requires a sorted array as input.
• Insertion and deletion operations are costly since the array needs to
be maintained in a sorted order.
• Binary search is not suitable for unsorted arrays.
Use Cases
• Searching for an element in a phonebook.
• Finding a specific word in a dictionary.
• Implementing autocomplete functionality.
Conclusion
• Binary search is a powerful algorithm for efficient searching in sorted
arrays.
• It reduces the search space in each iteration, resulting in a logarithmic
time complexity.
• Understanding binary search can help optimize search operations in
various applications.

an Introduction to binary search algorithm

  • 1.
  • 2.
    Introduction • Binary searchis a divide-and-conquer algorithm used for searching in a sorted array. • It efficiently finds the position of a target value within the array.
  • 3.
    How Binary SearchWorks • Binary search starts by comparing the target value with the middle element of the array. • If the middle element is the target, the search is successful. • If the target is smaller, the search continues on the left half of the array. • If the target is larger, the search continues on the right half of the array. • This process is repeated until the target is found or the search range becomes empty.
  • 4.
    Algorithm Steps • Startwith a sorted array and a target value. • Set the search range to the entire array. • Calculate the middle index of the search range. • Compare the middle element with the target value. • If they are equal, the search is successful. • If the target is smaller, update the search range to the left half of the array and go to step 3. • If the target is larger, update the search range to the right half of the array and go to step 3. • Repeat steps 3-7 until the target is found or the search range is empty.
  • 5.
    Implementation • It canbe implemented using iteration as well as recursion
  • 6.
    Algorithm Steps int binarySearch(intarray[], int x, int start, int end) { // Iterative Binary Search // Repeat until the pointers start and end meet each other while (start <= end) { int mid = (start + start) / 2; if (array[mid] == x) return mid;
  • 7.
    Algorithm Steps if (array[mid]< x) start = mid + 1; else end = mid - 1; } return -1; }
  • 8.
    Algorithm Steps // RecursiveBinary Search int binarySearchRecursive(int arr[], int left, int right, int target) { if (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] > target) return binarySearchRecursive(arr, left, mid - 1, target); else return binarySearchRecursive(arr, mid + 1, right, target); } return -1; }
  • 9.
    Time Complexity • Binarysearch has a time complexity of O(log n), where n is the number of elements in the array. • It reduces the search space by half in each iteration, leading to a logarithmic time complexity.
  • 10.
    Advantages of BinarySearch • Efficient searching algorithm for sorted arrays. • Works well even with large datasets. • Requires fewer comparisons compared to linear search.
  • 11.
    Limitations of BinarySearch • Requires a sorted array as input. • Insertion and deletion operations are costly since the array needs to be maintained in a sorted order. • Binary search is not suitable for unsorted arrays.
  • 12.
    Use Cases • Searchingfor an element in a phonebook. • Finding a specific word in a dictionary. • Implementing autocomplete functionality.
  • 13.
    Conclusion • Binary searchis a powerful algorithm for efficient searching in sorted arrays. • It reduces the search space in each iteration, resulting in a logarithmic time complexity. • Understanding binary search can help optimize search operations in various applications.