liveBook · Manning

Chapter 2: Selection sort

What you will learn in chapter 2 of Grokking Algorithms, Second Edition

The chapter introduction explains why sorted data matters and why learning selection sort is worthwhile even when sorting is built in.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction explains why sorted data matters and why learning selection sort is worthwhile even when sorting is built in.

Key terms: sorted list, binary search, selection sort, quicksort, sorting algorithm

2.1 How memory works

Introduces computer memory as addressed storage slots and names arrays and linked lists as two ways to store multiple items.

Key terms: memory, address, arrays, linked lists

2.2 Arrays and linked lists

This section explains why adding items to an array can require moving it in memory, and how reserving extra space helps but has costs.

Key terms: array, contiguously, chunk of memory, slots, memory

2.3 Linked lists

This section explains how linked lists connect items stored at separate memory addresses and why that makes insertion and memory allocation easier than with arrays.

Key terms: linked lists, address, next item, inserts, previous item, arrays, linked list, slots, memory

2.4 Arrays

This section contrasts an array’s direct access to items with the step-by-step traversal needed to reach an item in a linked list.

Key terms: arrays, linked lists, address, random elements

2.5 Terminology

Defines an array element’s index and explains why array numbering starts at zero.

Key terms: array, element, index

2.6 Inserting into the middle of a list

The section compares middle insertion in linked lists and arrays, then explains what a linked-list pointer stores.

Key terms: linked lists, arrays, insert elements, shift, pointer, address, linked list

2.7 Deletions

This section compares deletion in linked lists and arrays, explains when deletion takes O(1) time, and contrasts deletion with insertion.

Key terms: deletions, arrays, linked lists, O(1) time, first and last items, linked list, insertions, space left in memory

2.8 Which is used more, arrays or linked lists?

Explains why arrays are used more often than linked lists, comparing access speed and memory use while noting exceptions.

Key terms: random access, sequential access, arrays, linked lists, caching, wasted space, extra memory per item, pointers

2.9 Selection sort

The section explains how selection sort builds a ranked list by repeatedly choosing the next highest item and why its running time is O(n²).

Key terms: selection sort, sorted list, play count, O(n), O(n2), big O notation, constants

2.10 Example code listing

The section introduces a code example for sorting an array from smallest to largest, but the supplied text does not include the code or explain how it works.

2.11 Recap

The recap contrasts how arrays and linked lists store elements and which operations each supports efficiently.

Key terms: array, linked list, address, arrays, linked lists, reads, inserts, deletes

Where this is used later

Concepts explained in this chapter

← Chapter 1: Introduction to algorithms Chapter 3: Recursion →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.