next up previous contents
Next: Specification of our search Up: Introduction Previous: Fundamental questions of interest   Contents

An example: searching

One important problem in computing is that of locating information. More succinctly, this is problem is known as searching. The information to be searched has to be represented (or encoded) somehow. This is were data structures come in. As you have already seen, in a computer, everything is ultimately represented as a sequence of binary digits (bits). But this is too low level for our purposes. What we wish is to develop and study powerful data structures which are closer to the way we think (or at least more structured than mere sequences of bits). After all, it is us, humans, who have to develop software systems -- computers merely run them.

After we have chosen a suitable representation, the information has to be processed. In this case, the process is that of searching. This is where algorithms come in. To begin with, let's consider

  1. One particular representation and a variation, and
  2. two algorithms for that representation.
In order to simplify matters, let's suppose that we want to search a collection of integer numbers (we could have equally well deal with strings of characters or any other data type of interest). As you have already seen, arrays are one of the simplest possible ways of representing collections.

It will be convenient to be able to write down arrays on paper. We just write its items in order, separated by commas and enclosed by square brackets. Thus,

$\displaystyle [1,4,17,3,90,79,4,6,81] $

is an example of an array. Let's call this array $ a$:

$\displaystyle a = [1,4,17,3,90,79,4,6,81] $

Then the array $ a$ has $ 9$ items, and hence we say that its size is $ 9$. In everyday life, we usually start counting from $ 1$. When we work with arrays, we more often start from 0. Thus, for our array $ a$, its positions are $ 0,1,2,\dots,7,8$. The element in the $ 8^{\rm th}$ position is 81. Here we use the notation $ a[8]$ to denote this element. More generally, for any integer $ i$ denoting a position, we write $ a[i]$ to the denote the element in the $ i^{\rm th}$ position. This position $ i$ is called an index (the plural is indices). Then, for example, $ a[0] = 1$ and $ a[2] =
17$.

If we ask where $ 17$ is in the array $ a$, the answer is $ 2$. If we ask where $ 91$ is, the answer is nowhere. Because we are not using $ -1$ as a possible index (as we start counting from 0), we may, as we do, use the number $ -1$ to represent nowhere. Other (perhaps better) conventions are possible, but we'll stick to this here.


next up previous contents
Next: Specification of our search Up: Introduction Previous: Fundamental questions of interest   Contents
Martin Escardo 2005-01-11