Quick summary
A data structure is a way of organising and storing data in memory so that it can be used efficiently. Choosing the right data structure makes programs faster and simpler. This topic covers the classification of data structures, abstract data types, basic operations, and analysis of algorithms using time and space complexity and asymptotic notations.
Key points
- Data structure = organised data + allowed operations on it.
- Primitive data structures: int, float, char, pointer (directly supported by the machine).
- Non-primitive: built from primitive ones. Divided into linear (array, stack, queue, linked list – elements in a sequence) and non-linear (tree, graph – hierarchical or network form).
- Static structures have fixed size (array); dynamic structures can grow or shrink at run time (linked list).
- Basic operations: traversal, insertion, deletion, searching, sorting, merging.
- Abstract Data Type (ADT): a logical description of data and operations, without saying how they are implemented. Example: a Stack ADT has push, pop, peek, isEmpty.
- An algorithm is analysed for time complexity (number of steps as a function of input size n) and space complexity (memory needed).
- Cases: best case, average case and worst case. Worst case is used most often because it gives a guarantee.
- Asymptotic notations: Big-O (upper bound), Big-Omega Ω (lower bound), Big-Theta Θ (tight bound).
- Growth order (slow to fast): O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(n³) < O(2ⁿ) < O(n!).
- Time–space trade-off: we can often save time by using more memory, or save memory at the cost of time.
- Applications: arrays for tables, stacks for function calls and undo, queues for printers and CPU scheduling, trees for file systems and databases, graphs for maps and networks.
Important terms
- Data structure (डेटा संरचना) – organised way of storing data.
- ADT – model of data and operations independent of implementation.
- Time complexity – how running time grows with input size.
- Space complexity – how memory use grows with input size.
- Big-O notation – upper bound on growth rate, f(n) = O(g(n)) if f(n) ≤ c·g(n) for all n ≥ n₀.
Formulas
- f(n) = O(g(n)) if there exist c > 0 and n₀ such that f(n) ≤ c·g(n) for all n ≥ n₀
- f(n) = Ω(g(n)) if f(n) ≥ c·g(n) for all n ≥ n₀
- f(n) = Θ(g(n)) if c₁·g(n) ≤ f(n) ≤ c₂·g(n) for all n ≥ n₀
- Drop constants and lower terms: 3n² + 5n + 2 = O(n²)
| Operation | Complexity |
|---|---|
| Access array element by index | O(1) |
| Linear search | O(n) |
| Binary search (sorted) | O(log n) |
| Bubble sort (worst) | O(n²) |
| Merge sort | O(n log n) |
5 important questions
Q1. Differentiate linear and non-linear data structures.
In linear structures elements form a sequence and each element has at most one predecessor and one successor (array, stack, queue, linked list). In non-linear structures an element can connect to many others in a hierarchy or network (tree, graph).
Q2. What is an Abstract Data Type? Give an example.
An ADT defines what data is stored and what operations are allowed, but not how they are coded. Example: Queue ADT with enqueue, dequeue, front and isEmpty; it can be implemented with an array or a linked list.
Q3. Find the time complexity of this code: for (i = 0; i < n; i++) for (j = 0; j < n; j++) sum++;
- Outer loop runs n times.
- For each outer iteration, inner loop runs n times.
- Total executions of sum++ = n × n = n².
- Time complexity = O(n²).
Q4. Find the time complexity of: for (i = 1; i < n; i = i * 2) count++;
- i takes values 1, 2, 4, 8, … 2ᵏ.
- Loop stops when 2ᵏ ≥ n, i.e. k ≥ log₂ n.
- So the loop runs about log₂ n times.
- Time complexity = O(log n).
Q5. Explain asymptotic notations with graphs in words.
- Big-O: f(n) stays below c·g(n) after n₀; gives the worst-case upper bound. Graph: f(n) curve lies under c·g(n) to the right of n₀.
- Omega: f(n) stays above c·g(n) after n₀; lower bound (best case).
- Theta: f(n) is sandwiched between c₁·g(n) and c₂·g(n); exact growth.
- Example: 3n² + 5n is O(n²), Ω(n²) and Θ(n²).
Exam tip
Practise finding the complexity of simple loops; it is a common 4-mark question. Learn the growth order list by heart.