CS Pathfinder Logo CS Pathfinder

Unit 1 · Foundation of DSA

Array (1D)

Learn one-dimensional arrays in DSA with declaration, memory representation, traversal, insertion, deletion, searching, and C examples.

Introduction

A one-dimensional array is a linear data structure that stores elements of the same type in continuous memory locations.

Arrays are one of the first and most important data structures because they teach indexing, traversal, searching, and memory layout.

If you understand arrays properly, many later topics such as strings, matrices, stacks, queues, and heaps become easier.

Index:  0    1    2    3    4
Value: 10   20   30   40   50

Why Arrays Are Important

Fast Access

Elements can be accessed directly using index in O(1) time.

Simple Storage

Data is stored in a continuous block of memory.

Foundation Topic

Many advanced structures use arrays internally.

Easy Traversal

Loops make it simple to visit every element.

Table of Contents

What is a 1D Array?

A 1D array stores multiple values under a single variable name. Each value is accessed using an index.

In C, array indexing starts from 0. So the first element is at index 0, the second at index 1, and so on.

Formula: Address of arr[i] = Base Address + i * size of each element.

Declaration and Initialization

An array must have a data type, name, and size. Values can be assigned at declaration time or later.

  • int marks[5]; declares an integer array of size 5.
  • int marks[5] = {90, 80, 70, 85, 95}; declares and initializes the array.
  • marks[0] accesses the first element.

Basic Operations on 1D Array

Common array operations include traversal, insertion, deletion, searching, and updating.

  • Traversal: Visit every element one by one.
  • Insertion: Add a new element at a position by shifting elements.
  • Deletion: Remove an element and shift remaining elements.
  • Searching: Find an element by checking values.
  • Updating: Change value at a given index.

Advantages and Limitations

Arrays are simple and fast for indexed access, but they are not always flexible.

  • Advantage: O(1) direct access using index.
  • Advantage: Easy to use with loops.
  • Limitation: Fixed size in basic C arrays.
  • Limitation: Insertion and deletion may require shifting elements.

Array Operation Complexity

Operation Time Complexity Reason
Access by index O(1) Direct address calculation
Traversal O(n) Visit all elements
Search unsorted array O(n) May check every element
Insertion at middle O(n) Shifting required
Deletion at middle O(n) Shifting required

C Program Example

This program traverses a 1D array and prints all elements.

#include <stdio.h>

int main() {
    int arr[5] = {10, 20, 30, 40, 50};

    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

Output: 10 20 30 40 50

Key Points

  • A 1D array stores same-type elements in continuous memory.
  • Array index starts from 0 in C.
  • Access by index takes O(1) time.
  • Insertion and deletion may take O(n) because of shifting.

Interview Tip

Always check array bounds. Accessing arr[n] in an array of size n is invalid because the last index is n - 1.

Common Beginner Mistakes

  • Using index 1 as the first position in C arrays.
  • Accessing elements outside array bounds.
  • Forgetting to shift elements during insertion or deletion.
  • Confusing array size with last valid index.

Quick Revision

  • First index is 0.
  • Last index is size - 1.
  • Direct access is O(1).
  • Traversal is O(n).

Summary

A one-dimensional array is a simple linear data structure that stores elements in continuous memory. It provides fast index-based access and forms the base for many important DSA concepts.

Data Structures & Algorithms Handwritten Notes

Master DSA with 142 Pages of Easy Handwritten Notes – Perfect for Interviews, Placements, GATE & Exams.