資料結構›Ch1 演算法基礎第 10 題/共 57 題
10. Loop Invariant、二元搜尋
#DS-01-010中Loop Invariant二元搜尋
Consider an integer array of size with elements , where the first elements are positive and the other elements are negative. The following algorithm calculates correctly by calling COMPUTE-P(A,n). What loop invariant is maintained for the while loop of the algorithm?
COMPUTE-P(A, len) 1 if A[1] < 0 return 0 2 left = 1, right = len 3 while left < right 4 m = ceiling((left+right)/2) 5 if A[m] > 0 6 left = m 7 else 8 right = m - 1 9 return left
📄 台大111
▤完整推導請見《WH 資工筆記 · 資料結構》Ch1 演算法基礎