Session 23 of Software Lessons, “Programming in C++: Data Structures and Sorting Algorithm Examples,” runs about 22 minutes.
What this session covers
- Data structure examples
- Vectors (a dynamic array)
- Ordered maps (key-value pairs)
- Unordered maps (key-value pairs)
- Hash maps (key-value pairs)
- Sets (unique, ordered elements)
- Queues (first in, first out)
- Stacks (last in, first out)
- Deques (double-ended queues)
- Pairs (a first and second element)
- Structs (custom data structures)
- Sorting algorithm examples
- Bubble sort, O(n²): swap higher and lower neighbors as required
- Selection sort, O(n²): select the minimum and put it at the front
- Merge sort, O(n log n): recursive split, comparison and merge
- The many other sorting algorithms
Key ideas
- A queue removes items in the order they were added, while a stack removes the most recently added item first.
- A map stores key-value pairs, and a hash map uses a hash function to find a key’s location quickly on average.
- A set holds each element only once.
- Bubble sort and selection sort compare many pairs of elements, which makes their running time grow with the square of the input size.
- Merge sort splits the data in half, sorts each half and merges them, which gives O(n log n) time.