MCS-208 · Unit 2

Arrays, Pointers and Structures

Section 8 of 8

Block 1 · Unit 2Arrays, Pointers and Structures7 min · Section 8 of 8

2.8Applications of Arrays: Strengths and Limits

Covers IGNOU MCS-208 §2.6

Arrays are the default container in most programs, but they are not always the best one. This page sums up what arrays do well and badly, with the cost of each operation, so you can judge when another structure (such as the linked lists of Unit 3) is better.

Cost of the basic operations

For an array of nn elements:

OperationTimeWhy
Read or write a[i]O(1)O(1)Address = start + ii × size
Search in an unsorted arrayO(n)O(n)May have to check every element
Search in a sorted arrayO(log⁡n)O(\log n)Binary search (Unit 1)
Insert at position iiO(n)O(n)Elements i…n−1i \dots n-1 move one place right
Delete at position iiO(n)O(n)Elements i+1…n−1i+1 \dots n-1 move one place left
Insert or delete at the endO(1)O(1)Nothing moves (if there is room)
Change the sizeNot possibleA C array has a fixed size; you must allocate a new one and copy

Advantages and disadvantages

AdvantagesDisadvantages
O(1)O(1) access to any element by indexFixed size: chosen in advance, too small or wasted
Simple to declare, traverse, search and sortInsertion and deletion in the middle cost O(n)O(n)
No extra memory per element (no links)Needs one contiguous block of memory
Neighbouring elements are close in memory, which suits the cacheAll elements must have the same type

Where arrays are used

Arrays are the right choice when the number of items is known in advance (or changes rarely), and when insertions and deletions in the middle are rare. Typical uses:

  • tables and matrices (marks of a class, images as grids of pixels, sparse-matrix triples);
  • lookup tables: precomputed answers reached by index, e.g. the prefix sums of Unit 1;
  • the storage inside other data structures: stacks, queues, heaps and hash tables are often built on arrays (Units 4, 5, 9 and 10);
  • strings and buffers for input and output.

Key takeaways

  • Access by index is O(1)O(1); search is O(n)O(n), or O(log⁡n)O(\log n) if the array is sorted.
  • Insert and delete in the middle are O(n)O(n) because elements shift.
  • Arrays suit fixed-size data with few insertions and deletions.
  • Many other structures (stacks, queues, heaps, hash tables) are built on arrays.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the worst-case number of element moves when inserting into an array that holds nn elements?

Show answer

Answer: C. nn

Inserting at position 0 shifts all nn elements one place right. Inserting at the end needs no moves, but that is the best case.

Question 2 · multiple choice

Which operation is O(1)O(1) for an array?

Show answer

Answer: A. Reading a[i] for a valid index i

Direct access uses the address formula. Deleting the first element shifts all the others, so it is O(n)O(n).

Question 3 · multiple choice

An array holds 1, 3, 5, 7, 9 (n = 5). After deleting the element at index 1, what are the contents and how many elements moved?

Show answer

Answer: B. 1 5 7 9, 3 moves

Index 1 holds 3. The elements 5, 7, 9 each move one place left: 3 moves. “1 3 7 9” deletes index 2 instead.

Question 4 · multiple choice

In which situation is an array the best choice?

Show answer

Answer: C. Marks of 60 students, read often by roll number

The size is known and access is by index, which arrays do in O(1)O(1). Frequent insertions in the middle or removals from the front cause shifting.

Question 5 · multiple choice

What does this program print?

C · q2-shift.c
#include <stdio.h>

int main(void)
{
    int a[6] = {4, 8, 15, 16, 23};
    int n = 5, pos = 2;
    for (int i = n; i > pos; i--)
        a[i] = a[i - 1];
    a[pos] = 42;
    n++;
    for (int i = 0; i < n; i++)
        printf("%d ", a[i]);
    printf("\n");
    return 0;
}
Show answer

Answer: D. 4 8 42 15 16 23

The loop moves 23, 16 and 15 one place right (starting from the end), then 42 goes into index 2. “4 8 42 16 23 0” is what you get if 15 is overwritten instead of shifted.

Question 6 · short answer

List three advantages and three disadvantages of arrays, and give the time complexity of access, insertion and deletion.

Show worked answer

Advantages: (1) any element is reached in O(1)O(1) by its index; (2) arrays are simple to declare, traverse, search and sort; (3) no extra memory is needed for links, and neighbouring elements are close in memory. Disadvantages: (1) the size is fixed in advance, so memory is wasted or runs out; (2) inserting or deleting in the middle needs shifting, O(n)O(n); (3) all elements must have the same type and the array needs one contiguous block. Complexity: access O(1)O(1); insertion and deletion O(n)O(n) in the worst case (O(1)O(1) at the end).

↑ ↓ move↵ openTab switch type