Shortcut for chapter specific information

Wednesday, July 27, 2011

Will I ever forget this?

I just chatted with my colleagues on using sentinel of linear search.  In my mind, I always assume one can use a negative infinity as a sentinel.

But when one of colleagues come back to me and ask "Doesn't that mean you still have two comparisons? "  Then I was dumbfounded.   And his is right! I was dead wrong!

As it turns out, one should actually use the key as the sentinel.  In that case, one just need to check the return index is smaller than the length of the array or the length of the array +1 at the end of the search to decide if the search is sucessful.

This is a good mistake, I think I will never forget this trick from now on.

No comments:

Post a Comment