Chapter 1 · Introduction
Learning Objectives
- Define data structures and algorithms
- Classify linear vs. non‑linear structures
- Distinguish static vs. dynamic memory
- Explain ADTs and basic operations
- Analyze time & space complexity
- Apply Big‑O, Omega, and Theta notations
1. Data Structures Basics
Data structure = a structural representation of the logical relationship between data elements. It organises data to increase process efficiency.
Goals
- Correctness – works for all valid inputs.
- Efficiency – fast processing & minimal memory usage.
Basic Operations
- Traversing – visit each element
- Searching – locate an item
- Insertion – add a new element
- Deletion – remove an element
- Sorting – arrange in order
- Merging – combine two structures
2. Static vs Dynamic Linear Structures
🔒 Static (Fixed)
Memory allocated at compile‑time. Size cannot change.
- ❌ Wasted memory if underused
- ❌ Inefficient insertion/deletion (shifting)
🔓 Dynamic (Resizable)
Memory allocated at runtime. Can grow/shrink.
- ✅ Space‑efficient (allocates as needed)
- ✅ Efficient insertion/deletion (e.g., linked lists, vectors)
Simulate: Static vs Dynamic (click to see the behaviour)
3. ADT & Algorithms
Abstract Data Type (ADT) = a collection of data items together with the operations on that data. The implementation is hidden behind a "wall" — the user only sees what it does, not how.
Properties of an Algorithm
- Unambiguous – each step is clear.
- Input – 0 or more well‑defined inputs.
- Output – 1 or more outputs.
- Finiteness – terminates after finite steps.
- Feasibility – possible with available resources.
- Independent – language‑agnostic.
4. Complexity Analysis
Time Complexity – how runtime grows with input size n.
Space Complexity – memory usage.
Cases
- Best – minimum time (e.g., first element found).
- Average – expected time.
- Worst – maximum time (the guaranteed upper bound).
Growth Rate Table
| Complexity | Name | Example |
|---|---|---|
| O(1) | Constant | Accessing array index |
| O(log n) | Logarithmic | Binary search |
| O(n) | Linear | Finding in unsorted array |
| O(n log n) | Linearithmic | Merge sort |
| O(n²) | Quadratic | Nested loops |
| O(2ⁿ) | Exponential | Towers of Hanoi |
for (int i = 1; i <= n; i *= 2) { sum++; }5. Asymptotic Notations
Big‑O (O)
Upper bound (worst‑case).
\( f(n) \le c \cdot g(n) \) for \( n \ge n_0 \).
Omega (Ω)
Lower bound (best‑case).
\( f(n) \ge c \cdot g(n) \) for \( n \ge n_0 \).
Theta (Θ)
Tight bound (both upper and lower).
\( c_1 \cdot g(n) \le f(n) \le c_2 \cdot g(n) \).
6. Chapter Activities (with Solutions)
Attempt each question, then click "Reveal Solution" to check your understanding.
Q1 Asymptotic Notations
Determine Big‑O, Omega, and Theta for:
- \( f(n) = 4n^3 + 2n^2 + n + 1 \)
- \( g(n) = \log(n^2) \)
- \( h(n) = n^2 \cdot \log(n) \)
- \( i(n) = 3^n \)
1. \( O(n^3), \Omega(n^3), \Theta(n^3) \)
2. \( O(\log n), \Omega(\log n), \Theta(\log n) \) (since \( \log(n^2)=2\log n \))
3. \( O(n^2\log n), \Omega(n^2\log n), \Theta(n^2\log n) \)
4. \( O(3^n), \Omega(3^n), \Theta(3^n) \) (exponential)
Q2 Growth Rate Functions
Arrange in increasing order of growth rate:
- \( f(n) = n^2 \)
- \( g(n) = n \cdot \log n \)
- \( i(n) = 2^n \)
Reason: logarithmic factor grows slower than linear, and exponential dominates polynomial.
Q3 Comparing Algorithms
Algorithm A: \( O(n \log n) \) | Algorithm B: \( O(n^2) \).
Which has better asymptotic performance and why?
Q4 Runtime of Code Segments
Determine Big‑O for each:
for (int i=0; i<n; i++) cout << i;for (int i=1; i<=n; i*=2) for (int j=0; j<m; j++) ...(assume m constant)for (int i=0; i<n; i++) for (int j=0; j<i; j++) sum++;
1. \( O(n) \) – linear single loop.
2. \( O(\log n) \) – outer doubles (log n), inner runs constant times.
3. \( O(n^2) \) – inner runs \( 0+1+2+\dots+(n-1) = n(n-1)/2 \), which is quadratic.
Q5 C++ Review: Array/Pointer & Struct/Class
a) Show inserting and traversing 5 integers using an array and a pointer.
b) Create a StudentRecord (ID, name, GPA) and store 5 records, then display them.