25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

PROCEDURE search (x: INTEGER; VAR root: Word); (* ADenS432_List4 *)<br />

VAR w1, w2: Word;<br />

BEGIN<br />

w1 := root; sentinel.key := x;<br />

IF w1 = sentinel THEN (*first element*)<br />

NEW(root); root.key := x; root.count := 1; root.next := sentinel<br />

ELSIF<br />

w1.key = x THEN INC(w1.count)<br />

ELSE (*search*)<br />

REPEAT w2 := w1; w1 := w2.next<br />

UNTIL w1.key = x;<br />

IF w1 = sentinel THEN (*new entry*)<br />

w2 := root;<br />

NEW(root); root.key := x; root.count := 1; root.next := w2<br />

ELSE (*found, now reorder*)<br />

INC(w1.count);<br />

w2.next := w1.next; w1.next := root; root := w1<br />

END<br />

END<br />

END search<br />

The improvement in this search method strongly depends on the degree of clustering in the input data.<br />

For a given factor of clustering, the improvement will be more pronounced for large lists. To provide an<br />

idea of how much gain can be expected, an empirical measurement was made by applying the above<br />

cross-reference program to a short <strong>and</strong> a relatively long text <strong>and</strong> then comparing the methods of linear list<br />

ordering <strong>and</strong> of list reorganization. The measured data are condensed into Table 4.2. Unfortunately, the<br />

improvement is greatest when a different data organization is needed anyway. We will return to this<br />

example in Sect. 4.4.<br />

Test 1 Test 2<br />

Number of distinct keys 53 582<br />

Number of occurrences of keys 315 14341<br />

Time for search with ordering 6207 3200622<br />

Time for search with reordering 4529 681584<br />

Improvement factor 1.37 4.70<br />

Table 4.2. Comparsion of List Search Methods.

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

Saved successfully!

Ooh no, something went wrong!