Loop invariant of linear search

前端 未结 7 1768
醉梦人生
醉梦人生 2021-01-31 09:16

As seen on Introduction to Algorithms (http://mitpress.mit.edu/algorithms), the exercise states the following:

Input: Array A[1..n] and a v

7条回答
  •  臣服心动
    2021-01-31 09:22

    In the case of linear search, the loop variant will be the backing store used for saving the index(output) .

    Lets name the backing store as index which is initially set to NIL.The loop variant should be in accordance with three conditions :

    • Initialization : This condition holds true for index variable.since, it contains NIL which could be a result outcome and true before the first iteration.
    • Maintenance : index will hold NIL until the item v is located. It is also true before the iteration and after the next iteration.As, it will be set inside the loop after comparison condition succeeds.
    • Termination : index will contain NIL or the array index of the item v.

    .

提交回复
热议问题