Searching & Sorting
Data Structures Cheat Sheet
Every structure and algorithm from CSE 102 on one page: operation costs, when to use what, key formulas and the programs to practise.
Operation costs
| Structure | Access | Search | Insert | Delete | Notes |
|---|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) | O(n) | O(1) at the end |
| Sorted array | O(1) | O(log n) | O(n) | O(n) | binary search |
| Singly linked list | O(n) | O(n) | O(1)* | O(1)* | *at head or after a known node |
| Doubly linked list | O(n) | O(n) | O(1)* | O(1)* | delete with only the node pointer |
| Stack | top only | O(n) | O(1) push | O(1) pop | LIFO |
| Queue | front only | O(n) | O(1) enqueue | O(1) dequeue | FIFO |
| BST (balanced) | — | O(log n) | O(log n) | O(log n) | O(n) if skewed |
| AVL tree | — | O(log n) | O(log n) | O(log n) | guaranteed |
| Binary heap | O(1) max/min | O(n) | O(log n) | O(log n) extract | priority queue |
| Hash table | — | O(1) avg | O(1) avg | O(1) avg | O(n) worst |
| Graph (adj. list) | — | O(V + E) traversal | O(1) edge | O(deg) edge | sparse graphs |
Choosing a structure
| You need to… | Use |
|---|---|
| access by position, rarely insert | array |
| insert/delete often in the middle | linked list |
| undo, backtrack, match brackets, evaluate expressions | stack |
| process in arrival order, BFS, scheduling | queue |
| always get the smallest/largest next | heap / priority queue |
| keep keys sorted with fast search | balanced BST (AVL) |
| look up by key, fastest on average | hash table |
| model connections or networks | graph |
Key formulas
| Fact | Formula |
|---|---|
| Max nodes at level l of a binary tree | 2ˡ |
| Max nodes in a binary tree of height h | 2ʰ⁺¹ − 1 |
| Min height for n nodes | ⌈log₂(n + 1)⌉ − 1 |
| Heap/complete tree in an array (0-based) | parent (i−1)/2, children 2i+1, 2i+2 |
| AVL balance factor | height(left) − height(right) ∈ {−1, 0, 1} |
| Edges in a tree with n vertices | n − 1 |
| Sum of degrees (undirected) | 2 × edges |
| Max edges, simple undirected graph | n(n − 1) / 2 |
| Load factor of a hash table | α = n / m |
| Circular queue next index | (i + 1) % capacity |
Sorting summary
| Algorithm | Average | Worst | Stable | In place |
|---|---|---|---|---|
| Bubble / Insertion | O(n²) | O(n²) | yes | yes |
| Selection | O(n²) | O(n²) | no | yes |
| Merge | O(n log n) | O(n log n) | yes | no |
| Quick | O(n log n) | O(n²) | no | yes |
| Heap | O(n log n) | O(n log n) | no | yes |
| Counting | O(n + k) | O(n + k) | yes | no |
Programs to practise
- Insert/delete at a position in an array.
- Singly linked list: insert at head, tail and position; delete by value; reverse; find middle; detect a cycle.
- Doubly and circular linked list insert and delete.
- Stack using an array and a linked list; balanced brackets; infix → postfix; postfix evaluation.
- Circular queue; queue using a linked list.
- Binary tree traversals (recursive), height, count leaves.
- BST insert, search, delete; check if a tree is a BST.
- Heap insert and extract; heap sort.
- BFS and DFS on an adjacency matrix and an adjacency list.
- Hash table with chaining and with linear probing.
- Linear and binary search; all six sorts.
Theory questions to prepare
- Classify data structures with examples; define ADT.
- Array vs linked list.
- Stack and queue applications; why a circular queue?
- Convert and evaluate an expression (show the stack table).
- Tree terminology; types of binary trees; traversals of a given tree.
- Construct a BST / AVL tree from a key sequence; show rotations.
- Heapify an array; trace heap sort.
- Adjacency matrix vs list; BFS vs DFS with a traced example.
- Hash functions and collision resolution; load factor.
- Compare sorting algorithms (table above); define stable and in-place.
Final check: mixed revision
4 questions · pick an answer to check it instantly
- 1
Which structure would you use to check for balanced parentheses?
Each closing bracket must match the most recent unmatched opening bracket — LIFO. - 2
What is the postorder traversal of a BST built by inserting 8, 3, 10, 1, 6?
Left subtree (1, 6, then 3), right subtree (10), then the root 8. - 3
Which needs the array to be sorted?
Binary search relies on order to discard half the range. - 4
A max-heap's array is
[50, 40, 45, 10, 20]. After extracting the max, what is the new root?20 moves to the root and sifts down past its larger child 45.