0% found this document useful (0 votes)
36 views

Binary Search - Bubble Sort - Algrithm and Data Structure

Binary search is an algorithm for finding an element's position in a sorted array. It works by repeatedly dividing the search interval in half. The array must be sorted first. The algorithm begins by comparing the target value to the middle element of the array. If they are unequal, the half in which the target cannot lie is eliminated. This process continues with the subsequent half, and so on, until the target is found or the remaining half is empty. Binary search has a best-case time complexity of O(1) and average and worst-case complexities of O(log n). It uses constant O(1) space.

Uploaded by

Orang Biasa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Binary Search - Bubble Sort - Algrithm and Data Structure

Binary search is an algorithm for finding an element's position in a sorted array. It works by repeatedly dividing the search interval in half. The array must be sorted first. The algorithm begins by comparing the target value to the middle element of the array. If they are unequal, the half in which the target cannot lie is eliminated. This process continues with the subsequent half, and so on, until the target is found or the remaining half is empty. Binary search has a best-case time complexity of O(1) and average and worst-case complexities of O(log n). It uses constant O(1) space.

Uploaded by

Orang Biasa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Binary Search

Data Structure and Algorithm


Binary Search?
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
1. The array in which searching is to be performed is:

Let x = 4 be the element to be searched.


Binary Search Working
2. Set two pointers low and high at the lowest and the highest positions
respectively.

Setting pointers
Binary Search Working
3. Find the middle element mid of the array ie. arr[(low + high)/2] = 6.

Mid element
Binary Search Working
3. If x == mid, then return mid.Else, compare the element to be searched with m.

4. If x > mid, compare x with the middle element of the elements on the right side of
mid. This is done by setting low to low = mid + 1.

5. Else, compare x with the middle element of the elements on the left side of mid.
This is done by setting high to high = mid - 1.
Binary Search Working
7. Repeat steps 3 to 6 until low meets high.
Binary Search Working
8. x = 4 is found.
In Coding General
Iteration Method
In Python In PYTHON
Binary Search Complexity
Time Complexities

● Best case complexity: O(1)


● Average case complexity: O(log n)
● Worst case complexity: O(log n)

Space Complexity

The space complexity of the binary search is O(1).


Binary Search Applications
● In libraries of Java, .Net, C++ STL
● While debugging, the binary search is used to pinpoint the place where the
error happens.
References
● Python.org
● The Python Book
● w3school.com
● programiz.com
Thank You

You might also like