Version: 0.3.38

4. Searching#

Lectures:

Lecture 2.4 (slides)

Objectives:

Understand how “order” enables searching faster in sequences.

Concepts:

Sequences (ADT), jump search (algorithm), binary search (algorithm), interpolation search (algorithm)

When we looked at sequences, we specified the function seq.search() that returns a position where the given value can be found.

The algorithm we looked at is called a linear search or sequential search. It compares each item of the sequence with the desired one, and returns the position of the first match. If there is no match, it returns a special “not found” value (-1, 0, etc.).

Without knowing more about the given sequence, this is the best we can do. There is no magic, and to find the position of an item in a sequence of \(n\) items takes \(O(n)\) in the worst case.

To go faster, we know to assume that the given sequence is sorted, that is, that the item come in ascending (or descending order). Consider for instance the following examples:

  • \(s_1 = (2, 4, 7, 10, 11, 37, 42)\) is sorted in ascending order, i.e., from the smallest to the largest.

  • \(s_2 = (x, s, o, g, f, c, a)\) is sorted in descending order (alphabetical)

  • \(s_3 = (4, 6, 3, 10, 23, 1, 2)\) is not sorted.

In this lecture, we shall look at three “search” algorithm, which assume a sorted sequence, namely the jump search, the binary search and the interpolation search. Table 4.3 compares all these searching algorithms.

Table 4.3 Search algorithms#

Algorithm

Worst Case

Average Case

Assumptions

Linear Search

\(O(n)\)

\(O(n)\)

None

Jump Search

\(O(\sqrt(n))\)

\(O(\sqrt(n))\)

Sorted

Binary Search

\(O(\log_2 n)\)

\(O(\log_2 n)\)

Sorted, Bounded

Interpolation Search

\(O(n)\)

\(O(\log_2 \log_2 n)\)

Sorted, Bounded, Uniformly distributed

Important

Why does it help to know that the sequence is sorted? Consider for example the sequence shown in Fig. 4.1. Say we are looking for the position of the value 67. We look at position 7, and we find 56. Since we know that items are in ascending order, we can therefore exclude positions 1 to 6. If 67 is in the sequence, it is necessarily after position 7.

../_images/search_sorted.png

Fig. 4.1 Deciding on where to continue searching, when the given sequence is sorted.#