# Search Algorithms

*Summary:* We consider a typical problem of computing and a variety of algorithms used to solve that problem.

## Introduction

To *search* a data structure is to examine its elements one-by-one until either (a) an element that has a desired property is found or (b) it can be concluded that the structure contains no element with that property. For instance, one might search a vector of integers for an even element, or a vector of pairs for a pair having the string `"elephant"`

as its cdr.

You’ve already encountered a number of forms of searching in Scheme. For example, you’ve searched lists using `assoc`

. You’ve also written more general procedures that find multiple elements with particular properties or that find elements based on more than just the car of an element list.

We’re now reading to think about a more general form of searching, one in which we specify the criterion for searching as a procedure value, rather than hard-coding the particular criterion in the structure of the search.

## Sequential Search

In a linear data structure – such as a flat list, a vector, or a file – there is an obvious algorithm for conducting a search: Start at the beginning of the data structure and traverse it, testing each element. Eventually one will either find an element that has the desired property or reach the end of the structure without finding such an element, thus conclusively proving that there is no such element. We used such a strategy for searching association lists. Here are a few alternate versions of the algorithm.

```
;;; Procedure:
;;; list-sequential-search
;;; Parameters:
;;; lst, a list
;;; pred?, a unary predicate
;;; Purpose:
;;; Searches the list for a value that matches the predicate.
;;; Produces:
;;; match, a value
;;; Preconditions:
;;; pred? can be applied to all values in lst.
;;; Postconditions:
;;; If lst contains an element for which pred? holds, match
;;; is one such value.
;;; If lst contains no elements for which pred? holds, match
;;; is false (#f).
(define list-sequential-search
(lambda (lst pred?)
(cond
; If the list is empty, no values match the predicate.
[(null? lst)
#f]
; If the predicate holds on the first value, use that one.
[(pred? (car lst))
(car lst)]
; Otherwise, look at the rest of the list
[else
(list-sequential-search (cdr lst) pred?)])))
;;; Procedure:
;;; vector-sequential-search
;;; Parameters:
;;; vec, a vector
;;; pred?, predicate
;;; Purpose:
;;; Searches the vector for a value that matches the predicate.
;;; Produces:
;;; match, a value
;;; Preconditions:
;;; pred? can be applied to all elements of vec.
;;; Postconditions:
;;; If vec contains an element for which pred? holds, match
;;; is the index of one such value. That is,
;;; (pred? (vector-ref vec match)) holds.
;;; If vec contains no elements for which pred? holds, match
;;; is false (#f).
(define vector-sequential-search
(lambda (vec pred?)
; Grab the length of the vector so that we don't have to
; keep recomputing it.
(let ([len (vector-length vec)])
; kernel: Keeps track of the position we're looking at.
(let kernel ([position 0]) ; Start at position 0
(cond
; If we've run out of elements, give up.
[(= position len)
#f]
; If the current element matches, use it.
[(pred? (vector-ref vec position))
position]
; Otherwise, look in the rest of the vector.
[else
(kernel (+ position 1))])))))
```

```
> (define sample (vector 1 3 5 7 8 11 13))
> (vector-sequential-search sample even?)
4 ; The position of 8
> (vector-sequential-search sample (right-section = 12))
#f
> (vector-sequential-search sample (left-section < 9))
5 ; The position of 11
```

### Alternative Return Values

These search procedures return `#f`

if the search is unsuccessful. The first returns the matched value if the search is successful. The second returns returns the position in the specified vector at which the desired element can be found. There are many variants of this idea: One might, for instance, prefer to signal an error or display a diagnostic message if a search is unsuccessful. In the case of a successful search, one might simply return `#t`

, if all that is needed is an indication of whether an element having the desired property is present in or absent from the list.

### Searching For Keyed Values

One of the most common “real-world” searching problems is that of searching a collection of compound values for one which matches a particular portion of the value, known as the *key*. For example, we might search a phone book for a phone number using a person’s name as the key or we might search a phone book for a person using the number as key. As you’ve probably noted, association lists implement this kind of searching, as long as we use the first value of a list as the key for that list.

If each value in the list or vector to search is a list, and the key is the first element of that list, and we are searching for strict equality, then we can use `assoc`

to search the list. However, if we might want to search using the second element as the key, or a combination of elements as the key, then we might want to make a `get-key`

procedure a parameter to our search procedure.

```
;;; Procedure:
;;; keyed-list-sequential-search
;;; Parameters:
;;; values, a list of compound values.
;;; get-key, a procedure that extracts a key from a compound value.
;;; key, a key to search for.
;;; Purpose:
;;; Finds a member of the list that has a matching key.
;;; Produces:
;;; match, a Scheme value
;;; #f, otherwise.
;;; Preconditions:
;;; The get-key procedure can be applied to each element of values.
;;; Postconditions:
;;; If there is no index for which
;;; (equal? key (get-key (list-ref values index)))
(define keyed-list-sequential-search
(lambda (values get-key key)
(list-sequential-search
values
(lambda (val) (equal? key (get-key val))))))
```

For example, consider the named objects from the lab on association lists. As you may recall, we represent each object as a list of values, the first of which is the name, the second of which is the type (ellipse or rectangle), the third of which is the color, the remaining specify the boundaries (left, right, top, bottom). To search by name, we use `car`

for `get-key`

. To search by type, we use `cadr`

for `get-key`

. To search by color, we use `caddr`

for `get-key`

.

```
; Search drawing for something named "vc"
> (keyed-list-sequential-search drawing car "vc")
; Search drawing for an ellipse
> (keyed-list-sequential-search drawing cadr "ellipse")
; Search drawing for something white
> (keyed-list-sequential-search drawing caddr "white")
```

## Binary Search

The sequential search algorithms just described can be quite slow if the data structure to be searched is large. If one has a number of searches to carry out in the same data structure, it is often more efficient to “pre-process” the values, sorting them and transferring them to a vector, before starting those searches. The reason is that one can then use the much faster *binary search* algorithm.

Binary search is a more specialized algorithm than sequential search. It requires a random-access structure, such as a vector, as opposed to one that offers only sequential access, such as a list. Binary search is limited to the kind of test in which one is looking for a particular value that has a unique relative position in some ordering. For instance, one could use a binary search to look for an element equal to 12 in a vector of integers ordered from smallest to largest, since 12 is uniquely located between integers less than 12 and integers greater than 12; but one wouldn’t use binary search to look for an even integer, since the even integers don’t have a unique position in any natural ordering of the integers.

Note that this means that we have to organize the vector based on the kind of value we want to search for. If we want to search a vector of named objects by name, we organize it alphabetically by name. If we want to search a vector of named objects by color, we organize it alphabetically by color. If we want to search a vector of named objects by width, we organize it numerically by width.

In binary search, we keep track of the vector, the value searched for, and the lower and upper bounds of the region still of interest. The key idea is to divide the region of interest of the sorted vector into two approximately equal parts, examining the element at the point of division to determine which of the parts must contain the value sought.

There are usually three possibilities for the relationship between the value sought and the element at the point of division.

- The key sought
*is*the key of the element at the point of division. The search has succeeded. - The key sought cannot follow the key of the element at the point of division in the ordering that was used to sort the vector. In this case, the value sought must be in a position with a lower index that the element at the point of division (if it is present at all) – in other words, it must be in the left half of the region of interest. The search procedure invokes itself recursively to search just the left half of that region.
- The value sought cannot precede the element at the point of division. In this case, the value sought must be in a higher-indexed position – in the right half of the region – if it is present at all. The search procedure invokes itself recursively to search just the right half of the region.

There is one other way in which the recursion can terminate: If, in some recursive call, the region to be searched contains no elements at all, then the search obviously cannot succeed and the procedure should take the appropriate failure action.

Here, then, is the basic binary-search algorithm. The identifiers `lower-bound`

and `upper-bound`

denote the starting and ending positions of the region of the vector within which the value sought must lie, if it is present at all. (We use the convention that the starting and ending positions are *inclusive* in that they are positions within the vector that we must include in the search.)

```
;;; Procedure:
;;; binary-search
;;; Parameters:
;;; vec, a vector to search
;;; get-key, a procedure of one parameter that, given a data item,
;;; returns the key of a data item
;;; may-precede?, a binary predicate that tells us whether or not
;;; one key may precede another
;;; key, a key we're looking for
;;; Purpose:
;;; Search vec for a value whose key matches key.
;;; Produces:
;;; match, a number.
;;; Preconditions:
;;; The vector is "sorted". That is,
;;; (may-precede? (get-key (vector-ref vec i))
;;; (get-key (vector-ref vec (+ i 1))))
;;; holds for all reasonable i.
;;; The get-key procedure can be applied to all values in the vector.
;;; The may-precede? procedure can be applied to all pairs of keys
;;; in the vector (and to the supplied key).
;;; The may-precede? procedure is transitive. That is, if
;;; (may-precede? a b) and (may-precede? b c) then it must
;;; be that (may-precede? a c).
;;; If two values are equal, then each may precede the other.
;;; Similarly, if two values may each precede the other, then
;;; the two values are equal.
;;; Postconditions:
;;; If vector contains no element whose key matches key, match is -1.
;;; If vec contains an element whose key equals key, match is the
;;; index of one such value. That is, key is
;;; (get-key (vector-ref vec match))
(define binary-search
(lambda (vec get-key may-precede? key)
; Search a portion of the vector from lower-bound to upper-bound
(let search-portion ([lower-bound 0]
[upper-bound (- (vector-length vec) 1)])
; If the portion is empty
(if (> lower-bound upper-bound)
; Indicate the value cannot be found
-1
; Otherwise, identify the middle point, the element at that
; point and the key of that element.
(let* ([midpoint (quotient (+ lower-bound upper-bound) 2)]
[middle-element (vector-ref vec midpoint)]
[middle-key (get-key middle-element)]
[left? (may-precede? key middle-key)]
[right? (may-precede? middle-key key)])
(cond
; If the middle key equals the value, we use the middle value.
[(and left? right?)
midpoint]
; If the middle key is too large, look in the left half
; of the region.
[left?
(search-portion lower-bound (- midpoint 1))]
; Otherwise, the middle key must be too small, so look
; in the right half of the region.
[else
(search-portion (+ midpoint 1) upper-bound)]))))))
```

### An Example

So, how do we use binary search to search a sorted vector? It depends on what the vector contains. Let’s suppose it contains a list of named objects, sorted by name. Here’s one such vector.

```
(define objects-by-name
(vector
(list "Amy" "ellipse" "blue" 90 50 25 5)
(list "Bob" "ellipse" "indigo" 80 40 35 30)
(list "Charlotte" "rectangle" "blue" 0 40 5 45)
(list "Danielle" "rectangle" "red" 0 140 35 15)
(list "Devon" "rectangle" "yellow" 80 0 10 5)
(list "Erin" "ellipse" "orange" 60 50 10 15)
(list "Fred" "ellipse" "black" 0 110 30 30)
(list "Greg" "ellipse" "orange" 110 10 35 50)
(list "Heather" "rectangle" "white" 100 140 35 50)
(list "Ira" "ellipse" "red" 100 100 5 50)
(list "Janet" "ellipse" "black" 60 70 5 20)
(list "Karla" "ellipse" "yellow" 20 110 25 10)
(list "Leo" "rectangle" "yellow" 60 40 30 50)
(list "Maria" "ellipse" "blue" 30 10 5 50)
(list "Ned" "rectangle" "yellow" 0 50 45 15)
(list "Otto" "rectangle" "red" 100 40 10 20)
(list "Paula" "ellipse" "orange" 100 20 50 25)
(list "Quentin" "ellipse" "black" 40 130 35 50)
(list "Rebecca" "rectangle" "green" 110 70 25 35)
(list "Sam" "ellipse" "white" 20 120 35 40)
(list "Ted" "rectangle" "black" 20 0 10 20)
(list "Urkle" "rectangle" "indigo" 40 110 10 5)
(list "Violet" "rectangle" "violet" 80 80 50 20)
(list "Xerxes" "rectangle" "blue" 60 130 25 35)
(list "Yvonne" "ellipse" "white" 40 110 50 40)
(list "Zed" "rectangle" "grey" 90 60 25 5)
))
```

Now, `binary-search`

has four parameters: a vector to search, the procedure that extracts a key from each element in the vector, the procedure used to compare keys, and a key to search for. For this example, the vector to search will be `objects-by-name`

and the name to search for will be whatever name we want. To get the name from an entry, we use `car`

. To compare two names, we use `string-ci<=?`

.

So, to find out the index of the entry for the object named “Heather”, we would write something like the following:

```
> (binary-search objects-by-name car string-ci<=? "Heather")
8
> (vector-ref objects 8)
("Heather" "rectangle" "white" 100 140 35 50)
```

To make it easier for people who don’t want to write so much, we might wrap that instruction into a more-specific procedure that looks up objects by name, returning objects, rather than indices.

```
;;; Procedure:
;;; lookup-object
;;; Parameters:
;;; objects, a list of named objects
;;; name, a string
;;; Purpose:
;;; Find the object associated with name.
;;; Produces:
;;; object, a named object (or #f)
;;; Preconditions:
;;; Each element of objects is a named object (a list of name, type,
;;; color, left, top, width, height).
;;; objects is arranged alphabetically by name, from alphabetically
;;; first to alphabetically last. That is,
;;; (string-ci<=? (car (vector-ref objects i))
;;; (car (vector-ref objects (+ i 1))))
;;; for all reasonable i.
;;; No two objects have the same name.
;;; Postconditions:
;;; If there is an i s.t. (car (vector-ref objects i)) is name, then
;;; object is (vector-ref objects i)
;;; Otherwise, object is #f
(define lookup-object
(lambda (objects name)
(let ([index (binary-search objects car string-ci<=? name)])
(if (= index -1)
#f
(vector-ref objects index)))))
```

Let’s watch it work.

```
> (lookup-object objects-by-name "Heather")
("Heather" "rectangle" "white" 100 140 35 50)
> (lookup-object objects-by-name "Sam")
("Sam" "ellipse" "white" 20 120 35 40)
> (lookup-object objects-by-name "Janet")
("Janet" "ellipse" "black" 60 70 5 20)
```

### Another Example: Searching for Primes

Let’s take a detour into a traditional mathematical problem: *Given a number, n, how do you decide if n is prime?* As you might expect, there are a number of ways to determine whether or not a value is prime. Since we know a lot of primes, for small primes the easiest technique is to search through a vector of known primes.

```
;;; Value:
;;; small-primes
;;; Type:
;;; vector of integers
;;; Contents:
;;; All of the prime numbers less than 1000, arranged in increasing order.
(define small-primes
(vector 2 3 5 7 11 13 17 19 23 29 31 37
41 43 47 53 59 61 67 71 73 79 83 89 97
101 103 107 109 113 127 131 137 139 149
151 157 163 167 173 179 181 191 193 197 199
211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293
307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397
401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499
503 509 521 523 541 547 557 563 569 571 577 587 593 599
601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691
701 709 719 727 733 739 743 751 757 761 769 773 787 797
809 811 821 823 827 829 839 853 857 859 863 877 881 883 887
907 911 919 929 937 941 947 953 967 971 977 983 991 997))
```

We could, of course, use a sequential search technique to look for a value in this vector. However, binary search is much more efficient. What procedure should we use for `get-key`

? Well, each value is its own key, so we use `(lambda (x) x)`

. The values are ordered numerically, so we use `<=`

for * may-precede?*.

For example,

```
; Is 231 a prime?
> (binary-search small-primes (lambda (x) x) <= 231)
-1 ; No
; Is 241 a prime?
> (binary-search small-primes (lambda (x) x) <= 241)
52 ; Yes, it's prime number 52
; How many primes are there less than 1000?
> (vector-length small-primes)
168
```

In procedure form, we might write

```
(define is-small-prime?
(lambda (candidate)
(not (= -1 (binary-search small-primes (lambda (x) x) <= candidate)))))
```

Now, how many recursive calls do we do in determining whether or not a candidate value is a small prime? If we were doing a sequential search, we’d need to look at all 168 primes less than 1000, so approximately 168 recursive calls would be necessary. In binary search, we split the 168 into two groups of approximately 84 (one step), split one of those groups of 84 into two groups of 42 (another step), split one of those groups into two groups of 21 (another step), split one of those groups of 21 into two groups of 20 (we’ll assume that we don’t find the value), split 10 into 5, 5 into 2, 2 into 1, and then either find it or don’t. That’s only about six recursive calls. Much better than the 168.

Now, suppose we listed another 168 or so primes. In sequential search, we would now have to do 336 recursive calls. With binary search, we’d only have to do one more recursive call (splitting the 336 or so primes into two groups of 168).

This slow growth in the number of recursive calls (that is, when you double the number of elements to search, you double the number of recursive calls in sequential search, but only add one to the number of recursive calls in binary search) is one of the reasons that computer scientists love binary search.

### Verifying That a Vector is Sorted

For `binary-search`

to work correctly, we need to have a sorted vector. Checking that a vector is sorted will require looking at every neighboring pair of values, so it is not something we want to do every time we call binary search. However, it is helpful to have such a procedure available.

```
;;; Procedure:
;;; vector-sorted?
;;; Parameters:
;;; vec, a vector
;;; get-key, a procedure that extracts keys from the elements of vec
;;; may-precede?, a procedure that compares keys
;;; Purpose:
;;; Determine if vec is sorted by key
;;; Produces:
;;; is-sorted?, a Boolean
;;; Preconditions:
;;; get-key should be applicable to any value in vec.
;;; may-precede? should be applicable to any two values returned by get-key.
;;; Postconditions:
;;; If, for all reasonable i,
;;; (may-precede? (get-key (vector-ref vec i))
;;; (get-key (vector-ref vec (+ i 1))))
;;; then is-sorted is #t.
;;; Otherwise,
;;; is-sorted is #f.
(define vector-sorted?
(lambda (vec get-key may-precede?)
(let ([veclen (vector-length vec)])
(letrec ([kernel (lambda (i)
(or (= i (- veclen 1))
(and (may-precede? (get-key (vector-ref vec i))
(get-key (vector-ref vec (+ i 1))))
(kernel (+ i 1)))))])
(kernel 0)))))
```

Here are some tests for the vectors we defined earlier.

```
> (vector-sorted? small-primes id <)
#t
> (vector-sorted? objects-by-name car string-ci<=?)
#t
> (vector-sorted? objects-by-name cadr string-ci<=?)
#f
```

## Self Checks

These checks might take you a little bit longer, but they’re not complex. However, they are importantly designed to help you understand searching before starting the lab, so please make your best effort to complete them.

### Check 1: Where are My Keys?

a. Explain the role of the * pred?* parameter in

`list-sequential-search`

.b. Explain the role of the * get-key* parameter in

`keyed-list-sequential-search`

.c. Explain how these parameters work together to implement a keyed sequential search.

d. If we double the length of the list, what is the worst case effect on the number of recursive calls in `list-sequential-search`

?

### Check 2: Binary Search

a. Explain the role of the * may-precede?* in

`binary-search`

.b. Explain the role of `midpoint`

, `middle-element`

, `middle-key`

, `left?`

, and `right?`

, which are bound in the `let*`

of `binary-search`

.

c. Describe how and why the * upper-bound* of kernel

`search-portion`

changes when `left?`

is true.d. Describe how and why the * lower-bound* of kernel

`search-portion`

changes when `right?`

is true.e. If we double the length of the vector, what is the worst case effect on the number of recursive calls in `binary-search`

?

### Check 3: Observing Binary Search

a. Make a copy of `binary-search-lab.rkt`

.

b. Verify that binary search can correctly find the entry for `"Heather"`

in `objects-by-name`

. That is, write an expression to find `"Heather"`

.

```
> (binary-search objects-by-name ___ ___ "Heather")
```

*Hint:* If you can’t fill in the blanks yourself, the example is in the reading. We are asking you to run the search yourself.

c. Verify that binary search can correctly find the entry for an object of your choice in `objects-by-name`

.

d. Verify that binary search can correctly find the first entry in `objects-by-name`

. You will need to supply the name associated with that entry.

e. Verify that binary search can correctly find the last entry in `objects-by-name`

. You will need to supply the name associated with that entry.

f. Verify that binary search terminates and returns -1 for something that would fall in the middle of the vector and is not there. That is, pick a name that starts with M or N and that does not appear in the vector.

g. Verify that binary search terminates and returns -1 for something that comes before the first entry in `objects-by-name`

. You will need to pick a name that alphabetically precedes `"Amy"`

.

h. Verify that binary search terminates and returns -1 for something that comes after the last entry. You will need to pick a name that alphabetically follows `"Zaphod"`

.

i. Why do you think we had you verify each of these conditions?