資料結構›Ch1 演算法基礎
第 10 題/共 57 題
◀ DS 10/57
10. Loop Invariant、二元搜尋
#DS-01-010中Loop Invariant二元搜尋

Consider an integer array AA of size n>0n>0 with elements A[1],…,A[n]A[1],\ldots,A[n], where the first pp elements are positive and the other (n−p)(n-p) elements are negative. The following algorithm calculates pp 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 演算法基礎
本章題號 · 1–20 / 57