Книга: A Common-Sense Guide to Data Structures and Algorithms in Python, Volume 1
Назад: Measuring Speed
Дальше: Searching

Reading

The first operation we’ll look at is reading, which looks up what value is contained at a particular index inside the array.

A computer can read from an array in just one step. This is because the computer has the ability to jump to any particular index in the array and peer inside. In our example of ["apples", "bananas", "cucumbers", "dates", "elderberries"], if we looked up index 2, the computer would jump right to index 2 and report that it contains the value "cucumbers".

How is the computer able to look up an array’s index in just one step? Let’s see how.

A computer’s memory can be viewed as a giant collection of cells. In the following diagram, you can see a grid of cells in which some are empty and some contain bits of data:

/books/45079/OEBPS/understanding_arrays/array_2.png

While this visual is a simplification of how computer memory works under the hood, it represents the essential idea.

When a program declares an array, it allocates a contiguous set of empty cells for use in the program. So if you were creating an array meant to hold five elements, your computer would find a group of five empty cells in a row and designate it to serve as your array:

/books/45079/OEBPS/understanding_arrays/array_3.png

Now, every cell in a computer’s memory has a specific address. It’s sort of like a street address (for example, 123 Main St.), except that it’s represented with a number. Each cell’s memory address is one number greater than the previous cell’s address. Here’s a visual that shows each cell’s memory address:

/books/45079/OEBPS/understanding_arrays/array_4.png

In the next diagram, you can see our shopping list array with its indexes and memory addresses:

/books/45079/OEBPS/understanding_arrays/array_5.png

When the computer reads a value at a particular index of an array, it can jump straight to that index because of the combination of the following facts about computers:

  1. A computer can jump to any memory address in one step. For example, if you asked a computer to inspect whatever’s at memory address 1063, it can access that without having to perform any search process. As an analogy, if I ask you to raise your right pinky finger, you wouldn’t have to search all your fingers to find which one is your right pinky. You’d be able to identify it immediately.

  2. Whenever a computer allocates an array, it also makes note at which memory address the array begins. So if we asked the computer to find the first element of the array, it would be able to instantly jump to the appropriate memory address to find it.

Now, these facts explain how the computer can find the first value of an array in a single step. However, a computer can also find the value at any index by performing simple addition. If we asked the computer to find the value at index 3, the computer would simply take the memory address at index 0 and add 3. (Memory addresses are sequential, after all.)

Let’s apply this to our grocery list array. Our example array begins at memory address 1010. So, if we told the computer to read the value at index 3, the computer would go through the following thought process:

  1. The array begins with index 0, which is at memory address 1010.
  2. Index 3 will be exactly three slots past index 0.
  3. By logical extension, index 3 would be located at memory address 1013, since 1010 + 3 is 1013.

Once the computer knows that index 3 is at memory address 1013, it can jump right there and see that it contains the value "dates".

Reading from an array is, therefore, an efficient operation, since the computer can read any index by jumping to any memory address in one step. Although I described the computer’s thought process by breaking it down into three parts, we are currently focusing on the main step of the computer jumping to a memory address. (In later chapters, we’ll explore how to know which steps are the ones worth focusing on.)

Naturally, an operation that takes just one step is the fastest type of operation. Besides being a foundational data structure, arrays are also a very powerful data structure because we can read from them with such speed.

Now, what if instead of asking the computer what value is contained at index 3, we flipped the question around and asked at what index "dates" can be found? That’s the search operation, and we’ll explore that next.

Назад: Measuring Speed
Дальше: Searching