Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 1: Detailed Component - Arrays

Lesson 2 of 14 in the free Data Structure notes on Siksha Sarovar, written by Rohit Jangra.

Arrays: A Deep Dive

Definition: An array is a finite, ordered collection of homogeneous (similar) data elements stored in contiguous (adjacent) memory locations.

Key Properties:

PropertyMeaningImplication
FiniteContains a specific number of elementsSize declared at creation, cannot grow
OrderedElements stored in sequential indicesDirect access via index (0, 1, 2...)
HomogeneousAll elements same data typeCannot mix int and string in same array
ContiguousAdjacent memory locationsEnables O(1) random access via address formula

Array Operations & Time Complexity

OperationTime ComplexityNotes
Access by IndexO(1)Direct calculation — greatest strength
Search (Unsorted)O(n)Must check each element
Search (Sorted)O(log n)Binary search possible
Insert at EndO(1)If space available
Insert at Beginning/MiddleO(n)Must shift elements right
Delete at EndO(1)Simply reduce size
Delete at Beginning/MiddleO(n)Must shift elements left

Representation and Address Calculation

Since arrays are stored contiguously, we can calculate the memory address of any element if we know the base address.

1. Linear Array (1D)

  • Formula: Address(A[K]) = Base(A) + w × (K - LB)
  • Base(A): Starting address of the array.
  • w: Size of each element in bytes (e.g., 4 bytes for int).
  • K: Index of the element to find.
  • LB: Lower Bound (Starting index, usually 0).

Worked Example:

Given: Base = 1000, w = 4 bytes, LB = 0. Find Address of A[5]. Address(A[5]) = 1000 + 4 × (5 - 0) = 1000 + 20 = 1020

2. Two-Dimensional Array (2D)

A 2D array is a "Matrix" with Rows and Columns. In memory, it is still stored linearly.

Storage OrderDescriptionFormula
Row-MajorRow by row (C, C++, Java)Base + w × [N × (i - Lr) + (j - Lc)]
Column-MajorColumn by column (Fortran, MATLAB)Base + w × [M × (j - Lc) + (i - Lr)]

(N = Number of Columns, M = Number of Rows, Lr/Lc = Lower Bounds)

Worked Example (Row-Major):

Given: A[3][4] array, Base = 2000, w = 2 bytes, LB = 0. Find Address of A[2][3]. N = 4 (columns) Address = 2000 + 2 × [4 × (2 - 0) + (3 - 0)] = 2000 + 2 × [8 + 3] = 2000 + 22 = 2022

3. Three-Dimensional Array (3D)

A 3D array is a collection of 2D arrays (Pages × Rows × Columns).

  • Concept: Think of a book (Pages), where each page has a grid of text (Rows, Cols).

Sparse Matrices

Definition: A Sparse Matrix is a matrix where the majority of elements are zero. Problem: Storing a standard 100×100 matrix with only 10 non-zero values wastes 9,990 memory spaces. Solution: Store only the non-zero elements using a "Triple" representation.

Triple Representation

Store each non-zero element as a 3-tuple: (Row, Column, Value).

Example: A 4×4 matrix with 3 non-zero values:

RowColValue
015
138
203

This stores 9 values instead of 16 — saving 43% memory. For large matrices the savings are enormous.

Special Matrices

TypeDefinitionConditionStorage Savings
DiagonalNon-zero only on main diagonalA[i][j] = 0 if i ≠ jStore only n elements
Tri-DiagonalValues on main, super, and sub diagonal`i - j≤ 1`Store 3n - 2 elements
Lower TriangularAll above diagonal are zeroA[i][j] = 0 if i < jStore n(n+1)/2 elements
Upper TriangularAll below diagonal are zeroA[i][j] = 0 if i > jStore n(n+1)/2 elements

Array Applications

ApplicationHow Arrays Are Used
Lookup TablesStore precomputed values for O(1) access
Image StoragePixels stored as 2D/3D arrays (RGB)
Dynamic ProgrammingMemoization tables for subproblem solutions
Hash TablesUnderlying storage for key-value pairs
Matrices in MLFeature vectors, weight matrices in neural networks