liveBook · Manning

Random access

As explained in Grokking Algorithms, Second Edition

Random access is the ability to retrieve an element directly by its position without sequentially traversing preceding elements.

Imagine opening a phone book to a known page instead of turning every page from the beginning. Random access lets you jump directly to an element by its position. Sequential access is different: you read elements one by one from the start. Linked lists generally require sequential access, while arrays provide random access. section 2.8

Finding the tenth element

To read the 10th element of a linked list, you must read the first nine elements and follow their links. With an array, you can jump straight to the 10th element because its position is directly calculable. section 2.8 section 2.4

Calculating an array position

Suppose an array has five items and begins at address 00. The fifth item is at address 04. Knowing the starting address and position makes direct lookup possible; linked-list elements may be scattered, so their links must be followed. section 2.4

Random access makes arrays useful when an application repeatedly jumps among elements. Arrays can also make sequential reads faster because nearby items can be read together through caching. Linked lists remain useful when items are read one at a time, but jumping around requires repeated link traversal. section 2.8 section 2.4

Use the memory-layout illustration to compare direct array positions with linked-list links. section 2.4

How Grokking Algorithms, Second Edition covers it

How deep it goes
Explains direct lookup, sequential traversal, address calculation, caching, and read trade-offs without implementation details.
Examples
Uses tenth-element lookup and an array beginning at address 00 whose fifth item occupies address 04.
What it assumes
Assumes basic familiarity with arrays, linked lists, elements, addresses, and reading items in order.
What you can do after
Evaluate/understand when arrays outperform linked lists for direct or repeated reads.
How it connects
Connects arrays, linked lists, sequential access, caching, and memory efficiency.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Algorithms, Second Edition. The section links point to the parts of the book it draws on.