25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 35<br />

1.8 Searching<br />

The task of searching is one of most frequent operations in computer programming. It also provides an<br />

ideal ground for application of the data structures so far encountered. There exist several basic variations of<br />

the theme of searching, <strong>and</strong> many different algorithms have been developed on this subject. The basic<br />

assumption in the following presentations is that the collection of data, among which a given element is to<br />

be searched, is fixed. We shall assume that this set of N elements is represented as an array, say as<br />

a: ARRAY N OF Item<br />

Typically, the type item has a record structure with a field that acts as a key. The task then consists of<br />

finding an element of a whose key field is equal to a given search argument x. The resulting index i,<br />

satisfying a[i].key = x, then permits access to the other fields of the located element. Since we are here<br />

interested in the task of searching only, <strong>and</strong> do not care about the data for which the element was searched<br />

in the first place, we shall assume that the type Item consists of the key only, i.e is the key.<br />

1.8.1 Linear Search<br />

When no further information is given about the searched data, the obvious approach is to proceed<br />

sequentially through the array in order to increase step by step the size of the section, where the desired<br />

element is known not to exist. This approach is called linear search. There are two conditions which<br />

terminate the search:<br />

1. The element is found, i.e. a i = x.<br />

2. The entire array has been scanned, <strong>and</strong> no match was found.<br />

This results in the following algorithm:<br />

i := 0; (* ADenS18_Search *)<br />

WHILE (i < N) & (a[i] # x) DO INC(i) END<br />

Note that the order of the terms in the Boolean expression is relevant.<br />

The invariant, i.e the condition satisfied before <strong>and</strong> after each loop step, is<br />

(0 ≤ i < N) & (Ak: 0 ≤ k < i : a k ≠ x)<br />

expressing that for all values of k less than i no match exists. Note that the values of i before <strong>and</strong> after each<br />

loop step are different. The invariant is preserved nevertheless due to the while-clause.<br />

From this <strong>and</strong> the fact that the search terminates only if the condition in the while-clause is false, the<br />

resulting condition is derived as:<br />

((i = N) OR (a i = x )) & (Ak: 0 ≤ k < i : a k ≠ x)<br />

This condition not only is our desired result, but also implies that when the algorithm did find a match, it<br />

found the one with the least index, i.e. the first one. i = N implies that no match exists.<br />

Termination of the repetition is evidently guaranteed, because in each step i is increased <strong>and</strong> therefore<br />

certainly will reach the limit N after a finite number of steps; in fact, after N steps, if no match exists.<br />

Each step evidently requires the incrementing of the index <strong>and</strong> the evaluation of a Boolean expression.<br />

Could this task be simplifed, <strong>and</strong> could the search thereby be accelerated? The only possibility lies in<br />

finding a simplification of the Boolean expression which notably consists of two factors. Hence, the only<br />

chance for finding a simpler solution lies in establishing a condition consisting of a single factor that implies<br />

both factors. This is possible only by guaranteeing that a match will be found, <strong>and</strong> is achieved by posting an<br />

additional element with value x at the end of the array. We call this auxiliary element a sentinel, because it<br />

prevents the search from passing beyond the index limit. The array a is now declared as

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!