next up previous contents
Next: A possible algorithm: linear Up: Introduction Previous: An example: searching   Contents

Specification of our search problem

We are now ready to formulate the specification of our search problem:

Given an array $ a$ and integer $ x$, find an integer $ i$ such that
  1. if there is no $ j$ such that $ a[j]$ is $ x$, then $ i$ is $ -1$,
  2. otherwise, $ a[i]$ is $ x$.
The first clause says that if $ x$ doesn't occur in the array then $ i$ should be $ -1$, and the second says that if it does then $ i$ should be a position where it occurs. If there is more than one position where $ x$ occurs, then this specification allows you to return any of them -- this would be the case e.g. if $ a$ were $ [17,13,17]$ and $ x$ were $ 17$. Thus, the specification is ambiguous. Hence different algorithms with different behaviours can satisfy the same specification -- for example, one algorithm may return the smallest position at which $ x$ occurs and an another the largest. There is nothing wrong with ambiguous specifications. In fact, they do occur in practice often.


next up previous contents
Next: A possible algorithm: linear Up: Introduction Previous: An example: searching   Contents
Martin Escardo 2005-01-11