NCL 408: Searching Arrays
So far, we've usually accessed an array because we already knew which element we wanted.
If a record has a stride of 2, we can calculate its address:
address = base + index × 2
But sometimes we don't know the index.
Suppose we have a small group of friends:
| Index | Name | Age |
|---|---|---|
0 |
NEMI | 34 |
1 |
ALICE | 29 |
2 |
BOB | 41 |
3 |
CHARLIE | 32 |
The records aren't sorted by age.
We want to answer a simple question:
Who is the oldest?
Finding the Oldest
We can start by treating Nemi as our current answer:
| Value | Current best |
|---|---|
| Age | 34 |
| Index | 0 |
Alice is 29:
29 > 34?
No. Nothing changes.
Bob is 41:
41 > 34?
Yes. Bob becomes our new best:
| Value | Current best |
|---|---|
| Age | 41 |
| Index | 2 |
Charlie is 32:
32 > 41?
No.
We've reached the end of the array, so our oldest index is 2.
That index leads us back to Bob's record.
Bob is the oldest.
Remembering the Best So Far
We didn't know the answer before we began.
Instead, we kept the best answer we'd seen so far and replaced it whenever we found a better one.
We needed to remember two things:
| Value | Why we need it |
|---|---|
| Oldest age | Compare it with the next age |
| Oldest index | Return to the winning record |
If we remembered only 41, we could answer:
What is the greatest age?
But our question was:
Who is the oldest?
The index lets us return to the complete record afterward.
Walking the Records
Let's store the table beginning at $100.
Each record has a stride of 2:
| Offset | Field |
|---|---|
0 |
Name |
1 |
Age |
So memory looks like:
| Address | Value |
|---|---|
$100 |
"NEMI" |
$101 |
34 |
$102 |
"ALICE" |
$103 |
29 |
$104 |
"BOB" |
$105 |
41 |
$106 |
"CHARLIE" |
$107 |
32 |
We'll use four working values:
| Alias | Purpose |
|---|---|
#index |
Current record index |
#address |
Address being calculated |
#oldest_age |
Greatest age seen so far |
#oldest_index |
Index where we found it |
Because the first record is already our starting answer, we can initialize from it:
#index $000
#address $001
#oldest_age $002
#oldest_index $003
#age [$001]
MOVE #oldest_age $101
MOVE #oldest_index 0
MOVE #index 1
The first record has already been considered, so the search begins at index 1.
For each remaining record, calculate its address:
:search
MUL #address #index 2
ADD #address #address 0x100
The Age field is one location after the beginning of the record:
INC #address
Now #age refers to the current age.
If it isn't greater than our current best, continue:
BLE :next #age #oldest_age
Otherwise, remember the new best:
MOVE #oldest_age #age
MOVE #oldest_index #index
Then move to the next record:
:next
INC #index
BLT :search #index 4
When the loop finishes, #oldest_index contains the index of the oldest person.
Initialize from real data
We could invent a starting value for #oldest_age, such as 0.
But that makes an assumption about the values we're going to search. The same algorithm used on an array containing only negative values would fail.
Using the first element as our initial best guarantees that our starting value actually belongs to the array.
From Index Back to Record
For our example:
| Value | Result |
|---|---|
| Oldest age | 41 |
| Oldest index | 2 |
We can calculate the beginning of record 2 exactly as before:
MUL #address #oldest_index 2
ADD #address #address 0x100
So:
#address = 0x104
That's Bob's Name field.
We searched using one field:
Age → record → Name
The Age field told us which record was interesting.
The Name field gave us the answer we actually wanted.
Sometimes we search using one part of a record because we want another part of the same record.
Looking for Someone
Now let's ask a different question:
How old is Alice?
This time, we already know what we're looking for:
"ALICE"
We just don't know which record contains it.
Start with the first record:
"NEMI" == "ALICE"?
No.
Move ahead by one stride:
"ALICE" == "ALICE"?
Yes.
We've found the record.
The Age field is one location later:
29
This time, the relationship went the other way:
Name → record → Age
The basic technique is still the same: walk the array, inspect a field, and use the matching record to reach the information we want.
But this search has something our first one didn't.
It can fail.
Searching Can Fail
When we asked who was oldest, there had to be an answer as long as the array contained at least one record.
One of those records had to contain the greatest age.
But suppose we ask:
How old is Doug?
We check each Name field:
"NEMI" == "DOUG"?
No.
"ALICE" == "DOUG"?
No.
"BOB" == "DOUG"?
No.
"CHARLIE" == "DOUG"?
No.
We've reached the end of the array.
Doug isn't there.
A search for a particular value therefore has two possible results:
| Result | Meaning |
|---|---|
| Matching index | We found the record |
-1 |
No matching record exists |
Since valid array indices begin at 0, -1 makes a convenient not found value.
Searching by Name
Let's implement that search.
We'll put the name we're looking for in #target:
#target $010
#index $000
#address $001
#name [$001]
S.MOVE #target "ALICE"
MOVE #index 0
For each record, calculate the address of its Name field:
:search
MUL #address #index 2
ADD #address #address 0x100
Now #name refers to the name in the current record.
If it matches, we're done:
S.BEQ :found #name #target
Otherwise, try the next record:
INC #index
BLT :search #index 4
If the branch doesn't happen, we've inspected every record without finding the name:
MOVE #index -1
JUMP :done
:found
-- #index already contains the matching index.
:done
After searching for "ALICE":
| Result | Value |
|---|---|
#index |
1 |
We can use that index to calculate Alice's record and read her Age field.
After searching for "DOUG":
| Result | Value |
|---|---|
#index |
-1 |
There is no record to read.
Check before using the result
-1 is not a valid array index.
If a search can fail, check its result before using that result to calculate an address.
Isn't this terribly slow?
It can be.
Our search starts at the beginning and checks records one at a time until it finds what it wants or reaches the end.
This is called a linear search.
In the worst case, the record we want is the final one—or isn't present at all—so we have to inspect every record.
That's not especially clever.
It is, however, extremely simple, works on an unordered collection, and is perfectly reasonable when the collection is small.
There are much faster ways to search some kinds of data. They usually require us to organize the data differently or maintain additional information about it.
Two Kinds of Search
We've used the same array to answer two different kinds of question.
To find the oldest person, we inspected every record and kept the best one we'd seen.
To find Alice, we began with a particular value and stopped when we found a matching record. If no record matched, the search failed.
In both cases, the field we inspected led us to a complete record.
That record could then give us something else:
Age → record → Name
or:
Name → record → Age
Try It
Our friends table is still:
| Index | Name | Age |
|---|---|---|
0 |
NEMI | 34 |
1 |
ALICE | 29 |
2 |
BOB | 41 |
3 |
CHARLIE | 32 |
Write a search that finds the youngest person.
Leave their index in #youngest_index.
Assume the array contains at least one record.
Show me one solution
Start with the first age as the youngest seen so far:
#index $000
#address $001
#youngest_age $002
#youngest_index $003
#age [$001]
MOVE #youngest_age $101
MOVE #youngest_index 0
MOVE #index 1
Then walk the remaining records:
:search
MUL #address #index 2
ADD #address #address 0x101
BGE :next #age #youngest_age
MOVE #youngest_age #age
MOVE #youngest_index #index
:next
INC #index
BLT :search #index 4
The result is:
| Value | Result |
|---|---|
| Youngest index | 1 |
Alice is the youngest.
We've seen how to walk an array when we don't already know the index we need, whether we're looking for the best record or searching for a particular value.
In the next lesson, NCL 409: Associative Arrays, we'll take the pattern of finding a record by one field and turn it into a data structure of its own.