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

Binary Search - v1

The document describes the binary search algorithm. It begins with defining the binary search problem of finding a target value in a sorted array. It then describes the trivial linear search solution with O(n) complexity and the goal of designing an O(logn) solution. The rest of the document discusses the binary search loop invariant which maintains that the array is sorted, the search space indices l and r satisfy l <= r, and the target x belongs within the search space. It details how the search space is narrowed on each iteration to eventually find the target or determine it is not present, terminating in O(logn) time.

Uploaded by

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

Binary Search - v1

The document describes the binary search algorithm. It begins with defining the binary search problem of finding a target value in a sorted array. It then describes the trivial linear search solution with O(n) complexity and the goal of designing an O(logn) solution. The rest of the document discusses the binary search loop invariant which maintains that the array is sorted, the search space indices l and r satisfy l <= r, and the target x belongs within the search space. It details how the search space is narrowed on each iteration to eventually find the target or determine it is not present, terminating in O(logn) time.

Uploaded by

Farrukh Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Binary Search

Binary Search Problem


• Given a sorted array of integers and a target value, find out if target
exists in the array or not

• Input: arr[] = {3,4,6,7}, target = 4

• Output: Target is in index 2

• Trivial Solution: Linear Search O(n) Complexity


Binary Search Problem
• Design O(logn) complexity algorithm
• Divide & Conquer
Binary Search Loop Invariant
• Three conditions
• Array arr is sorted in ascending order
• l <= r
• x belong to arr [l…..r]
Binary Search Loop Invariant
• Use loop invariant that the code is correct

• Initialization: The loop invariant has three parts


1. Array is sorted due to precondition of the method
2. Since arr.length is at least 1, thus l<=r
3. x is in arr b/c it is whole array and precondition guarantees that x is
in array
Binary Search Loop Invariant
• Maintenance: The loop invariant has three parts
1. Array arr is never changed so Case 1 is always true i.e. arr is sorted
2. Let l’ and r’ are the values of l and r at the end of 1st iteration, then
we need l’<r’ and x belongs to arr[l’…..r’]
3. Let m be the average of l and r, thus x belongs to arr[l…m] or arr
[m+1….j]
4. Case k belongs to arr[1….m]
must have x<=a[m] and thus if condition is true, then r’=m, l =1, this l’<r’ and
since x belongs to arr[1…m], by assumption its belong to arr[l’…..j’]
Binary Search Loop Invariant
• Maintenance: The loop invariant has three parts
5. Case k does not belong to arr[1….m]
must have x>a[m] and thus if condition is true, then r’=r, l =m+1, this l’<r’ and since x
belongs to arr[m+1…..r], by assumption its belong to arr[l’…..j’]

For the algorithm to be correct, arr[1] = x and happens only when l = r


• Termination: The value r−l is guaranteed to be non-negative. Because
integer division rounds down, it gets smaller on every loop iteration.
Therefore the loop eventually terminates

• More Detail:
https://www.cs.cornell.edu/courses/cs2112/2015fa/lectures/lec_loopinv/
Time Complexity
• T (n) = 1 + T(n/2)

• O(n)

You might also like