Choosing the right data structure often matters more than the algorithm itself. The method is simple: find the dominant operation, check the limits, take the simplest built-in type and verify the choice by counting operations. The previous lesson gave the ADT catalog; this lesson turns it into a decision rule.

A decision diagram from the dominant operation to the structure
A decision diagram from the dominant operation to the structure

Choosing by the dominant operation

Break the task into operations: search, insert, delete, order, min/max, a range query. The choice is decided by the most frequent operation on the largest n. Then look at the limits: memory, duplicates, whether keys are hashable, whether order matters. The costs of the main structures:

StructureFindInsertMin / order
listO(n)end O(1), middle O(n)O(n)
sorted listO(log n)O(n)O(1)
dequeO(n)both ends O(1)O(n)
set / dictO(1) averageO(1) averageO(n)
heapqO(n)O(log n)O(1)