Unit 1: Arrays, Searching Algorithms & Asymptotic Complexity Analysis
Memory address mapping mathematics, formal asymptotic limits (Big-O, Ω, Θ), Binary Search derivations, Interpolation search, and university exam problem breakdowns
Data Structures and Algorithms (DSA) represents the intellectual core of computer science and the primary filter used across technical engineering interviews. In the Second Year Semester 3 curriculum, Unit 1 establishes the bedrock: how contiguous memory buffers operate at hardware level, how algorithm efficiency is mathematically bounded using asymptotic calculus, and how search spaces are halved logarithmically through Binary and Interpolation Search. This chapter details memory addressing proofs, full C source code implementations, and autonomous university exam question solutions.
1. Contiguous Arrays & Multidimensional Address Mapping
An array is a contiguous collection of homogeneous data elements stored at successive memory addresses. Accessing an element at index i requires an \(O(1)\) arithmetic offset calculation from the array base address:
\[ \text{Address}(A[i]) = B + (i - \text{lower\_bound}) \times c \]
where \(B\) is the base address and \(c\) is the size of each element in bytes.
📐 2D Array Address Derivation Formulas (Compulsory Exam Questions)
For a 2D matrix \(A[M][N]\) with row range \([0 \dots M-1]\) and column range \([0 \dots N-1]\):
- Row-Major Order (C/C++ default): Consecutive elements of a row reside contiguously in memory.
\[ \text{Address}(A[i][j]) = B + [ (i \times N) + j ] \times c \] - Column-Major Order (Fortran/MATLAB default): Consecutive elements of a column reside contiguously in memory.
\[ \text{Address}(A[i][j]) = B + [ (j \times M) + i ] \times c \]
2. Asymptotic Notations and Formal Mathematical Bounds
Asymptotic analysis evaluates algorithm efficiency independent of machine hardware, compiler optimizations, or operating system quirks by analyzing behavior as input size \(n \to \infty\):
- Big-O Notation (\(O\)) — Asymptotic Upper Bound: \(f(n) = O(g(n))\) if there exist positive constants \(c\) and \(n_0\) such that \(0 \le f(n) \le c \cdot g(n)\) for all \(n \ge n_0\). Represents guaranteed worst-case execution performance.
- Big-Omega Notation (\(\Omega\)) — Asymptotic Lower Bound: \(f(n) = \Omega(g(n))\) if there exist positive constants \(c\) and \(n_0\) such that \(0 \le c \cdot g(n) \le f(n)\) for all \(n \ge n_0\). Represents best-case lower floor.
- Big-Theta Notation (\(\Theta\)) — Asymptotic Tight Bound: \(f(n) = \Theta(g(n))\) if there exist positive constants \(c_1, c_2\) and \(n_0\) such that \(0 \le c_1 \cdot g(n) \le f(n) \le c_2 \cdot g(n)\) for all \(n \ge n_0\). Encapsulates exact growth rate.
3. Linear Search: Algorithm & Complexity
Linear search sequentially inspects each element in an array from index \(0\) to \(n-1\) until the target key is located or the array boundary is reached.
- Best Case: Target is the first element → \(O(1)\).
- Worst Case: Target is at the final index or absent → \(O(n)\).
- Average Case: \(\frac{1}{n} \sum_{i=1}^n i = \frac{n(n+1)}{2n} = \frac{n+1}{2} \to O(n)\).
4. Binary Search: Divide-and-Conquer Logarithmic Search
Binary Search requires that the input array is strictly sorted. By comparing the target key with the middle element, it eliminates half of the remaining search space in every single step.
#include <stdio.h> int binarySearch(int arr[], int size, int target) { int low = 0; int high = size - 1; while (low <= high) { // Avoid integer overflow (low + high) / 2 int mid = low + (high - low) / 2; if (arr[mid] == target) { return mid; // Key located at index mid } else if (arr[mid] < target) { low = mid + 1; // Search right partition } else { high = mid - 1; // Search left partition } } return -1; // Key not found } int main() { int dataset[] = {11, 23, 38, 45, 56, 67, 79, 88, 94}; int n = sizeof(dataset) / sizeof(dataset[0]); int target = 67; int result = binarySearch(dataset, n, target); if (result != -1) printf("Element %d detected at array index: %d\n", target, result); else printf("Target element absent.\n"); return 0; }
Mathematical Proof of \(O(\log_2 n)\) Time Complexity
Let \(n\) be the number of elements. After iteration 1, remaining elements \(= \frac{n}{2}\). After iteration 2, \(= \frac{n}{2^2}\). After iteration \(k\), \(= \frac{n}{2^k}\). The search terminates when \(\frac{n}{2^k} = 1\):
\[ \frac{n}{2^k} = 1 \implies 2^k = n \implies k = \log_2 n \implies T(n) = \mathbf{O(\log_2 n)} \]
5. Interpolation Search for Uniformly Distributed Data
While Binary Search always checks the midpoint, Interpolation Search mimics human phone-book lookups by probing the likely target position based on key value:
\[ \text{pos} = \text{low} + \left[ \frac{\text{target} - \text{arr}[\text{low}]}{\text{arr}[\text{high}] - \text{arr}[\text{low}]} \right] \times (\text{high} - \text{low}) \]
For sorted and uniformly distributed data, Interpolation Search achieves average time complexity of \(\mathbf{O(\log_2(\log_2 n))}\). However, for skewed exponential distributions, worst-case performance degrades to \(O(n)\).
6. Comparative Matrix: Searching Algorithms
- Linear Search: Sorted requirement: No | Best Case: \(O(1)\) | Average Case: \(O(n)\) | Worst Case: \(O(n)\) | Auxiliary Space: \(O(1)\).
- Binary Search: Sorted requirement: Yes | Best Case: \(O(1)\) | Average Case: \(O(\log n)\) | Worst Case: \(O(\log n)\) | Auxiliary Space: \(O(1)\) (iterative).
- Interpolation Search: Sorted requirement: Yes (uniform) | Best Case: \(O(1)\) | Average Case: \(O(\log \log n)\) | Worst Case: \(O(n)\) | Auxiliary Space: \(O(1)\).
7. Typical Autonomous University Exam Questions
Q1: An array \(A[10 \dots 25][5 \dots 15]\) is stored in memory with base address 1200. If each element requires 4 bytes, calculate the address of \(A[18][12]\) in (i) Row-Major Order, and (ii) Column-Major Order. (6 Marks)
Step-by-Step Solution:
Row bounds: \(L_r = 10, U_r = 25 \implies M = 25 - 10 + 1 = 16\). Column bounds: \(L_c = 5, U_c = 15 \implies N = 15 - 5 + 1 = 11\). Given \(B = 1200, c = 4, i = 18, j = 12\).
(i) Row Major: \(\text{Addr} = 1200 + [ (18 - 10) \times 11 + (12 - 5) ] \times 4 = 1200 + [ 8 \times 11 + 7 ] \times 4 = 1200 + [88 + 7] \times 4 = 1200 + 380 = \mathbf{1580}\).
(ii) Column Major: \(\text{Addr} = 1200 + [ (12 - 5) \times 16 + (18 - 10) ] \times 4 = 1200 + [ 7 \times 16 + 8 ] \times 4 = 1200 + [112 + 8] \times 4 = 1200 + 480 = \mathbf{1680}\).
📥 Printable Chapter Reference (PDF Supplement)
Prefer an offline physical copy for revision or exam night preparation? You can download our verified, printer-friendly PDF synthesis for Unit 1 below:
🔗 Related Second Year Study Resources
- SY Semester 3 Academic Library — Full modules for Statistics, DSA, C++, and Graphics
- DSA Semester 3 Complete Roadmap — Algorithm design patterns and placement problem sets
- ETE Preparation Strategy — 10-day battle-tested revision roadmap for engineering exams