Lab6a is dedicated to different problems that could be solve by plain which in most cases is in time (linear time). We try to review and understand these problems and design better methods to solve these problems that are much faster than linear time. We are going to see how algorithm could help us come up with a solution that performances in logarithmic time. I present the basic algorithm of binary search for refernce here:
Algorithm 1 Binary Search
procedure BinarySearch(A[1...n],T)
L←0
R←0
while L<R do
m← floor((L+R)/2)
if A[m]<T then
L←m+1
else
R←m
end if
end while
return L
end procedure
The first question in the subsection part (a) asks when given an array of distinct integers that could be positive, negative, or zero that is sorted in increasing order such that . Provide a algorithm that finds an index such that or reports that no such index exists.
The solution by Jeff introduces a new computed array , which I describe here for my own knowledge and understaning. At first glance I didnt undertand the computation and the reason for this new array, Jeff states to initalize every element in the array as such for all index
Algorithm 2 Find Match
procedure FindMatch(ℓ,r)
if ℓ>r then
return None
end if
mid←(ℓ+r)/2
if A[mid]=mid then
return mid
else if A[mid]<mid then
return FindMatch(mid+1,r)
else
return FindMatch(ℓ,mid−1)
end if
end procedure
Since the algorithm is binary search the running time is