The array is the simplest data structure. It stores elements of one type in consecutive memory locations, so any element can be reached directly.

Definition and Classification

An array is a finite, ordered collection of elements of the same type, stored in contiguous memory and accessed by an index. In C, int a[5] declares an array of five integers, with indices 0 to 4. The number of elements is the size or length. Arrays are one-dimensional (linear), two-dimensional (tables or matrices) or multi-dimensional. They are static: the size is fixed when declared.

Definition: Array. A finite collection of elements of the same data type stored in consecutive memory locations and accessed by an index.

Representation in Memory

Since elements are consecutive, the address of any element can be computed. If the array starts at address Base, each element takes w bytes, and the lower bound of the index is LB, then Address of A[k] = Base + w × (k − LB). In C, LB is 0, so the address of a[k] is Base + w × k. This formula is why access by index takes O(1) time, whatever the size of the array. For int a[10] at base 1000 with 4-byte integers, a[6] is at 1000 + 4 × 6 = 1024.

Traversal, Insertion and Deletion

Traversal visits each element once, for example to print or sum them, and takes O(n). To insert an element at position pos in an array of n elements, the elements from the end back to pos are shifted one place right, and the new element is placed at pos. To delete the element at pos, the elements after it are shifted one place left. Both take O(n) time in the worst case, since many elements must move. Insertion needs free space at the end, otherwise it causes overflow.

Merging

Merging combines two sorted arrays into one sorted array. Two indices move through the arrays, and at each step the smaller element is copied to the result. When one array is used up, the rest of the other is copied. Merging arrays of sizes m and n takes O(m + n) time.

Table 1.3: Cost of array operations

Operation Time Reason
Access by index O(1) Address formula
Traversal O(n) Visit every element
Insertion / deletion O(n) Shift elements
Merge (sizes m, n) O(m + n) One pass of each

Example 1.3: Insertion and deletion

Problem: Array a = {10, 20, 30, 40, 50}. Insert 25 at index 2, then delete the element at index 4.

Solution:

  1. Shift 50, 40, 30 one place right, then put 25 at index 2: {10, 20, 25, 30, 40, 50}.
  2. Delete index 4 (the value 40) by shifting 50 left: {10, 20, 25, 30, 50}.

Result: The final array is {10, 20, 25, 30, 50}.

#include <stdio.h>
void show(int a[], int n) {
    for (int i = 0; i < n; i++) printf("%d ", a[i]);
    printf("\n");
}
int main(void) {
    int a[10] = {10, 20, 30, 40, 50}, n = 5, pos, i;
    pos = 2;
    for (i = n; i > pos; i--) a[i] = a[i - 1];
    a[pos] = 25; n++;
    show(a, n);
    pos = 4;
    for (i = pos; i < n - 1; i++) a[i] = a[i + 1];
    n--;
    show(a, n);
    return 0;
}

Output

10 20 25 30 40 50
10 20 25 30 50

Common mistakes

  • Shifting elements in the wrong direction when inserting, which overwrites data.
  • Accessing an index outside the array bounds. C does not check this.
  • Forgetting to update the count of elements after insertion or deletion.

Key takeaways

  • An array stores elements of one type in contiguous memory.
  • Address of A[k] = Base + w × (k − LB).
  • Access is O(1), insertion and deletion are O(n).
  • Merging two sorted arrays takes O(m + n).

Practice questions

  1. Give the address formula of a linear array.
  2. Why is insertion in an array O(n)?
  3. What is merging?

Answers to the practice questions

  1. Address of A[k] = Base + w × (k − LB).
  2. Because elements after the position must be shifted.
  3. Combining two sorted arrays into one sorted array.

Sources

  • Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit I