Discrete Binary Search in Topcoder Tutorial -
Discrete Binary Search in Topcoder Tutorial -
i went through code given in topcoder binary search tutorial.
binary_search(lo, hi, p): while lo < hi: mid = lo + (hi-lo)/2 if p(mid) == true: hi = mid else: lo = mid+1 if p(lo) == false: complain // p(x) false x in s! homecoming lo // lo to the lowest degree x p(x) true
i not able reason why low going point want i.e. lo to the lowest degree x p(x) true ?
i have tried on examples , comes out true not able think logically.
some sort of proof using invariant maintained in loop helpful .
thanks.
my understanding more of logical one:
if while loop has terminate lo
should become equal or greater hi
. assuming work integers, lo
equal hi
, hi
mid
.
binary-search
Comments
Post a Comment