문제

Imagine the following pseudocode

objects[i], 1 <= i <= n 

objects[0] = 0

for i from 1 to n
  if(objects[i] - objects[i-1] > constant)
    do something

I'd like to know if there's a specific name for the assignment objects[0] = 0. I know that when values like these are used to stop loops, they are called sentinel values. In this case, however, I'm using it so that the first object evaluated (objects[1]) will have something to compare against - obviously, objects[0] is not a real object, just sort of a flag. Is it still called a sentinel value? Is there another name for this? Or should I not be doing this at all?

Let me know if I haven't made myself clear and I should try to explain my question in another way.

도움이 되었습니까?

해결책

Cormen et al. writes in Introduction to Algorithms (3rd ed.) on page 238:

A sentinel is a dummy object that allows us to simplify boundary conditions.

This definition is broad enough to account for your usage (e.g. sentinel values of infinity are used in CLRS to simplify the merge routine of mergesort).

다른 팁

I've always called it a "sentinel" whether at the beginning or the end, and haven't yet been fired for that.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top