



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Given: A list of n elements a 1 , a 2 ,... , an in a non-decreasing order such that a 1 ≤ a 2 ≤... ≤ an and a number x. Objective: Check if x is present in the list, i.e., if ai = x for some i, 1 ≤ i ≤ n.
We apply binary search to get an O(log n) time algorithm. If x is present in the list, the algorithm returns the position of x. A position of −1 indicates x is not present. Note: a[i] is the ith ele- ment in the list, l is the left-most element, and r is the rightmost element. The initial call is BS(1,n).
BS(l,r)
⌊ (^) l+r 2
After each comparison we eliminate half the elements in our list. In the worst case, comparisons are made until there is one element remaining. The total number of comparisons is equal to the number of times that we can halve the list, which is the definition of the log function.
Given: A list of n elements a 1 , a 2 ,... , an in a non-decreasing order such that a 1 ≤ a 2 ≤... ≤ an and a number x. Objective: Find the left-most occurrence of x in the list.
We apply binary search to get an O(log n) time algorithm. However, we don’t return immediately after finding an occurrence of x. Instead, we note the “current” left-most position of x and continue searching for an occurrence that is further left. Note: c refers to the position of current left-most occurence. The initial call is LBS(1,n,−1).
LBS(l,r, c)
⌊ (^) l+r 2
Given: A list of n elements a 1 , a 2 ,... , an in a non-decreasing order such that a 1 ≤ a 2 ≤... ≤ an and a number x. Objective: Compute the number of occurrences of x in the list.
We apply binary search algorithms to find the leftmost and rightmost occurrences of x in the list. The difference plus one is returned as the count. RBS() finds the rightmost occurrence and OBS() computes the total number of occurrences.
RBS(l,r, c)
⌊ (^) l+r 2
OBS(l,r)
LBS() and RBS() are both O(logn) time algorithms. Therefore, OBS() is O(logn).
Given: Two lists of n elements a 1 , a 2 ,... , an and b 1 , b 2 ,... , bn in non-decreasing orders such that a 1 ≤ a 2 ≤... ≤ an and b 1 ≤ b 2 ≤... ≤ bn. Objective: Compute the median of the two lists, i.e., the nth smallest element.
MS(l 1 ,r 1 ,l 2 ,r 2 ,n)
l 1 +r 1 2
, m 2 ←
l 2 +r 2 2
⌊ (^) n 2
, r 2 = m 2.
⌊ (^) n 2
, r 1 = m 1.
⌊ (^) n+ 2
return MS(l 1 ,r 1 ,l 2 ,r 2 ,n)
We compare the medians of each list. Based on the result,
⌊ (^) n 2
elements are removed from each list. The
⌊ (^) n+ 2
smallest element is searched for among the remaining elements. The key observation is that the
⌊ (^) n 2
elements before the lesser median and the
⌊ (^) n 2
elements after the greater median may be discarded. The time complexity is O(log n).
3 log(n)+1^ = 3 · 3 log(n)^ = 3 · nlog(3)^ = O(nlog(3)) < O(n^2 ). When n is an odd number the sub- matrices will not be square. In such cases, the last row and the last column of the original matrix should be searched for x and removed in O(n) time prior to calling MS on the remaining square matrix. The sub-quadratic bound on the running time is not affected.
Given: A number x and a list of numbers such that the first n numbers are not equal to infin- ity and are in a non-decreasing order while all remaining numbers are equal to infinity. That is, a 1 ≤ a 2 ≤ · · · ≤ an < ∞ and an+1 = an+2 = · · · = ∞. Objective: Check if x is present in the list.
We find an infinity by iteratively checking the 2ith element beginning with i = 0. We then apply binary search on the list from 1 to 2i.
IS()
By construction, the last element that is not equal to infinity is between 2i−^1 and 2i, i.e., 2i−^1 ≤ n < 2 i. Therefore, 2i^ ≤ 2 n and our algorithm has O(logn) time complexity.
Given: An integer k and a list of numbers x 1 , x 2 ,... , xn such that xi < xi+1 < xi+2 < · · · < xn < x 1 < x 2 < · · · < xi− 1 for some i, 1 ≤ i ≤ n. Objective: Find the kth smallest element.
We apply a type of binary search to find the smallest element xi in the list. The key observation is that xl < xb l+r 2 c^ implies that xi is to the right of the median xb l+r 2 c
. The index of the kth smallest
element is returned by adding an offset to i.
SBS(l,r)
⌊ (^) l+r 2
We use a special remainder operator that has n%n = n. The time complexity is O(logn) because we do a binary search over the n elements.
Given: A number x and a list of numbers a 1 , a 2 ,... , an in a non-decreasing order such that a 1 ≤ a 2 ≤ · · · ≤ an. Objective: Find a pair of numbers ai and aj such that ai + aj = x.
With each comparison we eliminate one of the numbers in the list. When the sum is too low (high), the lowest (highest) number is removed.
SS(l,r)
The time complexity is O(n) because one comparison is made each call and one element is removed each call until only one element remains.
Given: Integers B, n, and M. Objective: Compute Bn^ mod M in O(log n) time.
We rely on the property that A × B mod M = (A mod M ) × (B mod M ) mod M. Below, % is the well-known mod operator (i.e., not the previously used special one).
MP(n)
The time complexity is O(logn) because there is one comparison for each recursive call until the exponent is reduced (by reducing by one half each time) to one.