Top Banner Ad

ACS201 — Unit 1: Introduction and Arrays

Table of Contents (15 sections)

ACS201 · Essentials of Data Structures · Unit 1

Introduction and Arrays

Unit Scope & Exam Weightage: This unit defines what a data structure is and why it matters, classifies structures, introduces Algorithm efficiency — Big O notation, and applies arrays: Array — insert, delete, search, traverse in C/Python and 2D array — matrix addition and transpose. The solutions file contains only generic 2026-27 model-paper templates for ACS201, not real past-paper questions, so every worked question here is explicitly labelled predicted and is derived from the exact syllabus topics and the unit's assignment problems.


Front Page: Importance-Wise Topic Roadmap (Predicted from Syllabus)

Priority Topic (Exact Syllabus) PYQs Asked Marks Weightage Tag
1 Array — insert / delete / search / traverse in C/Python 0 real PYQs predicted 7m Predicted Program
2 Algorithm efficiency — Big O notation: O(1), O(n), O(log n), O(n²) — concept only 0 real PYQs predicted 2m + 5m Predicted Theory
3 2D array — matrix addition and transpose Real connect:NumPy array (AI/ML) 0 real PYQs predicted 7m Predicted Program
4 Data structure — definition / need / classification (linear / non-linear, static / dynamic) 0 real PYQs predicted 2m + 5m Predicted Theory
5 image as 2D matrix integrated in #3 — Real Connect
6 sensor data log as array integrated in #1 — Real Connect
7 spreadsheet rows integrated in #1 — Real Connect
8 Introduction and Arrays (unit title/topic) integrated throughout — Unit Frame

2-Hour Last-Night Survival Checklist:

  1. Array access is O(1): address of a[i] is base + i × element_size, so indexing never scans. Insert/delete in the middle is O(n) because elements must shift.
  2. Insert trace: to insert 25 at index 2 in [10,20,30,40], shift 40→index4, 30→index3, write 25 at index 2: [10,20,25,30,40].
  3. Delete trace: to delete 30 from [10,20,30,40], shift 40 left into index 2 and reduce size: [10,20,40].
  4. Big O ladder: O(1) < O(log n) < O(n) < O(n²). Map them to array indexing, binary search, linear search/traversal, and nested matrix loops.
  5. 2D indexing: in C, a[i][j] is row i, column j; transpose uses t[j][i] = a[i][j]. Matrix addition requires equal dimensions.

1. Data Structure — Definition, Need and Classification

Data structure: a particular way of organising, storing and accessing data so that operations can be performed efficiently. It specifies the relationship among elements, the operations allowed, and the memory representation.

Need for data structures:

  1. Efficiency: the right structure reduces time (binary search O(log n) instead of linear O(n)) and memory waste.
  2. Organisation: real data has relationships—rows in a spreadsheet, links in a social network, nested folders in a file system—that arrays alone cannot model naturally.
  3. Reusability: standard structures (stack, queue, tree, graph) come with proven algorithms, so new problems can reuse them.
Classification Types Examples from this course
Linear / non-linear Elements in a sequence vs hierarchical/networked Array, linked list, stack, queue / tree, graph
Static / dynamic Size fixed at compile time vs grows at run time Array / linked list, NumPy list-backed structures
Homogeneous / heterogeneous Same type vs mixed types Array / structure (record)
%%{init: {'theme': 'base', 'look': 'handDrawn', 'themeVariables': {'fontFamily': "'Nunito', sans-serif", 'fontSize': '17px', 'primaryColor': '#EFEAD6', 'primaryBorderColor': '#0F766E', 'lineColor': '#23211C'}}}%%
flowchart TD
    DS[Data Structures] --> L[Linear]
    DS --> N[Non-linear]
    L --> S[Static: Array]
    L --> D[Dynamic: Linked List, Stack, Queue]
    N --> T[Tree]
    N --> G[Graph]

Examiner Tip: For a 2-mark “define data structure” answer, write one sentence plus one example. For 5 marks, add need and the two classification axes with one example per branch. Avoid a long history lesson.


2. Algorithm Efficiency — Big O Notation

Big O notation describes the upper bound of an algorithm's growth rate as input size n increases. It counts dominant operations, ignoring machine-specific constants.

O(1) constant  <  O(log⁡n) logarithmic  <  O(n) linear  <  O(n2) quadraticO(1) \text{ constant} \; < \; O(\log n) \text{ logarithmic} \; < \; O(n) \text{ linear} \; < \; O(n^2) \text{ quadratic}
Operation on array Method Complexity Why
Access a[i] direct address calculation O(1) base + i×size
Traverse all elements one loop O(n) visits each once
Linear search compare until match O(n) worst may scan whole array
Insert at end (space available) write at a[n], n++ O(1) no shifting
Insert at index k shift n-k elements O(n) shifting dominates
Binary search (sorted) halve range O(log n) range halves each step
Matrix multiply n×n three nested loops O(n²) for add/transpose; O(n³) for multiply nested iterations

Derivation of linear search cost: in the worst case the key is last or absent, so the loop runs n comparisons. Each comparison is constant work, giving T(n) = c·n + c₀ → O(n). For binary search, after k steps the remaining range is n/2^k; setting n/2^k = 1 gives k = log₂ n, hence O(log n).

Example for spreadsheet rows: looking up row 50 by index is O(1); finding the row whose name is “Ravi” is O(n); finding it in a sorted name column by binary search is O(log n).


3. Array Operations in C and Python

Array: a fixed-size, contiguous, homogeneous collection. In C, int a[5]; reserves 5 consecutive integers. In Python, the closest basic structure is list, e.g. a = [10,20,30,40]; NumPy provides a true fixed-type contiguous array.

3.1 C implementation

#include <stdio.h>
#define MAX 10
int main(void) {
    int a[MAX] = {10,20,30,40}, n = 4;
    int i, pos = 2, key = 25, found = -1;

    /* insert at index 2 */
    for (i = n; i > pos; i--) a[i] = a[i-1];
    a[pos] = key; n++;
    printf("After insert: ");
    for (i = 0; i < n; i++) printf("%d ", a[i]);   /* 10 20 25 30 40 */

    /* delete value 30 */
    for (i = 0; i < n; i++) if (a[i] == 30) found = i;
    if (found >= 0) {
        for (i = found; i < n-1; i++) a[i] = a[i+1];
        n--;
    }
    printf("\nAfter delete: ");
    for (i = 0; i < n; i++) printf("%d ", a[i]);   /* 10 20 25 40 */
    return 0;
}

3.2 Python implementation

a = [10, 20, 30, 40]
a.insert(2, 25)          # [10, 20, 25, 30, 40]
a.remove(30)             # [10, 20, 25, 40]
print(a[0], a[-1])       # 10 40  (negative index counts from end)
for x in a:              # traverse
    print(x)
print(25 in a)           # search: True
Operation C code idea Python list method Cost
Insert at index shift then write a.insert(k, x) O(n)
Delete at index shift left then n-- a.pop(k) / a.remove(x) O(n)
Traverse for(i=0;i<n;i++) for x in a: O(n)
Search loop with comparison x in a O(n)
Access a[i] a[i] O(1)

Sensor-data-log example: readings [23.5, 24.1, 22.8] stored as an array allow O(1) access to reading 2 and O(n) computation of the average. A fixed C array needs a declared capacity; a Python list grows automatically.


4. 2D Array — Matrix Addition and Transpose

2D array: an array of arrays, indexed by row and column. In C, int a[3][3] occupies 9 contiguous integers in row-major order: a[0][0], a[0][1], a[0][2], a[1][0], …. Address of a[i][j] is base + (i*cols + j)*element_size.

#include <stdio.h>
#define R 2
#define C 3
int main(void) {
    int a[R][C] = {{1,2,3},{4,5,6}};
    int b[R][C] = {{6,5,4},{3,2,1}};
    int s[R][C], t[C][R];
    int i, j;

    for (i=0;i<R;i++) for (j=0;j<C;j++) s[i][j] = a[i][j] + b[i][j];
    for (i=0;i<R;i++) for (j=0;j<C;j++) t[j][i] = a[i][j];

    printf("Sum:\n");
    for (i=0;i<R;i++) { for (j=0;j<C;j++) printf("%d ", s[i][j]); printf("\n"); }
    printf("Transpose of A:\n");
    for (i=0;i<C;i++) { for (j=0;j<R;j++) printf("%d ", t[i][j]); printf("\n"); }
    return 0;
}

Sum matrix is {{7,7,7},{7,7,7}}; transpose of A is {{1,4},{2,5},{3,6}}. Addition is valid only when both matrices have the same R×C; transpose changes shape to C×R.

Python/NumPy form (real connect):

import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print(a + np.array([[6,5,4],[3,2,1]]))  # [[7 7 7] [7 7 7]]
print(a.T)                              # [[1 4] [2 5] [3 6]]

An image is a 2D matrix of pixels (grayscale) or three stacked 2D matrices (RGB), which is why NumPy arrays underpin AI/ML image pipelines.

Examiner Tip: In a 7-mark 2D-array question, state the dimension condition before coding, then show both output matrices explicitly. Marks are commonly split: 1 for condition, 3 for loops, 2 for transpose, 1 for output.


5. Real Connect Table

Syllabus phrase Concrete interpretation
NumPy array (AI/ML) np.array stores homogeneous numeric data contiguously; ML libraries operate on these arrays
image as 2D matrix Each pixel is image[row][col]; a 512×512 grayscale image is a 512×512 array
sensor data log as array A time series of readings stored sequentially; index = sample number
spreadsheet rows Each row is a record; column-wise access is O(1) by index but search by value is O(n)

6. Predicted Exam-Ready Questions (Solved — Not Verbatim PYQs)

Predicted Q1 (2 Marks | Definition)

Q (predicted from “Data structure — definition”): Define data structure and give one example.

Solution: A data structure is a way of organising and storing data so that operations such as insertion, deletion, search and traversal can be performed efficiently. Example: an array stores a fixed number of same-type elements in contiguous memory and supports O(1) access by index.


Predicted Q2 (5 Marks | Need and Classification)

Q (predicted from “need” and “classification (linear / non-linear, static / dynamic)”): State the need for data structures and classify them with one example each.

Solution: Need: (1) Efficiency—binary search on a sorted array is O(log n) versus O(n) for linear search; (2) natural modelling of relationships—trees model folders, graphs model maps; (3) reusability of proven algorithms. Classification: linear (array, linked list, stack, queue) vs non-linear (tree, graph); static (array, fixed size) vs dynamic (linked list, grows at run time). A stack is linear and can be static (array implementation) or dynamic (linked implementation).


Predicted Q3 (7 Marks | Big O)

Q (predicted from “Algorithm efficiency — Big O notation: O(1), O(n), O(log n), O(n²) — concept only”): Explain O(1), O(n), O(log n) and O(n²) with one array-related example each.

Solution:

  • O(1): a[3] in an array; address computed directly from base index.
  • O(n): traverse a[0..n-1] to find the maximum; every element is visited once.
  • O(log n): binary search on a sorted array [10,20,30,40,50,60] for 50: compare middle 30, discard left half, compare 50 → 2 steps instead of 5.
  • O(n²): adding two n×n matrices uses two nested loops, n² additions. (Multiplying them is O(n³).) Ordering: O(1) < O(log n) < O(n) < O(n²).

Predicted Q4 (7 Marks | Array Insert/Delete Trace)

Q (predicted from assignment problem “Trace insertion at index 2 in [10,20,30,40]” and “Trace deletion of element 30 from [10,20,30,40]”): Perform both operations on the given array and show the array after each step.

Solution: Initial: index 0=10, 1=20, 2=30, 3=40, size=4. Insert 25 at index 2: shift 40 to index 4, 30 to index 3, write 25 at index 2, size=5. Result: [10, 20, 25, 30, 40]. Delete element 30: find it at index 3 after insertion; shift 40 from index 4 to index 3, size=4. Result: [10, 20, 25, 40]. If deletion is performed on the original array instead, result is [10, 20, 40]. Cost of each middle operation: O(n) due to shifting.


Predicted Q5 (7 Marks | 2D Array)

Q (predicted from “2D array — matrix addition and transpose”): For A = {{1,2},{3,4}} and B = {{5,6},{7,8}}, compute A+B and the transpose of A. Write the C loop used for transpose.

Solution: A+B = {{6,8},{10,12}} because each s[i][j] = a[i][j] + b[i][j]. Transpose of A = {{1,3},{2,4}}. C transpose loop:

for (i=0; i<rows; i++)
    for (j=0; j<cols; j++)
        t[j][i] = a[i][j];

The transpose has dimensions cols × rows; for a square matrix the shape is unchanged.


7. Practice Questions (Self-Test)

  1. (2 Marks) What is the address of a[4] if base = 1000 and each int occupies 4 bytes? (Ans: 1016.)
  2. (7 Marks) Write a C program to input N numbers, find max, min and average, and state the complexity of your approach.
  3. (5 Marks) Give two differences between static and dynamic data structures with examples.
  4. (7 Marks) Trace binary search for key 35 in [10,20,30,35,40,50], listing low, mid, high at each step.
  5. (7 Marks) Write Python code using NumPy to add two 2×3 matrices and print the transpose of the result.