Arrays December 19 ,2025

Array Operations and Time Complexity

A Complete Explanation of Operations Performed on Arrays

Introduction

Arrays are powerful because of their simple structure and predictable memory layout. However, every operation performed on an array has a cost, measured in terms of time and space complexity. Understanding these costs is essential for writing efficient programs and selecting the right data structure for a given problem.

This article explains all fundamental array operations, how they work internally, and how much time and memory they require.

Why Study Array Operations?

When working with arrays, you frequently perform operations such as:

  • Accessing elements
  • Traversing the array
  • Inserting new elements
  • Deleting elements
  • Searching for values
  • Updating elements

Each of these operations behaves differently due to the contiguous memory representation of arrays. Some operations are very fast, while others can be expensive.

Operations on Arrays

1. Accessing Elements in an Array

What Is Access Operation?

Accessing means retrieving the value stored at a specific index of an array.

Example

value = arr[5];

How It Works Internally

  1. The base address of the array is known.
  2. The memory address of the required element is calculated using:
Address = Base Address + (index × size of element)
  1. The value stored at that address is retrieved directly.

Time Complexity

O(1) — Constant Time

Why?

  • The address calculation takes constant time.
  • Access does not depend on array size.
  • Accessing the first or last element takes the same time.

2. Traversing an Array

What Is Traversal?

Traversal means visiting each element of the array at least once to perform an operation.

Example

for i = 0 to n-1:
    process(arr[i])

Internal Behavior

  • Elements are accessed sequentially
  • The loop runs once for each element

Time Complexity

O(n)

Reason

  • Every element must be visited
  • Time grows linearly with array size

3. Insertion Operation in Arrays

Insertion means adding a new element at a specified position.

a) Insertion at the End

Case 1: Space Available

  • Insert element at the last index

Time Complexity:
O(1) (Best Case)

Case 2: Array Full (Dynamic Arrays)

  • New memory allocated
  • Existing elements copied

Time Complexity:
 O(n)

b) Insertion at the Beginning

Steps

  1. Shift all elements one position to the right
  2. Insert new element at index 0

Time Complexity:
 O(n)

Reason:
All elements must be shifted.

c) Insertion at a Given Index

Steps

  1. Shift elements from the insertion index onward
  2. Insert the new element

Time Complexity:
 O(n)

Reason:
Shifting elements is costly.

4. Deletion Operation in Arrays

Deletion means removing an element from a specified index.

a) Deletion from the End

Steps

  • Remove last element
  • Reduce size

Time Complexity:
 O(1)

b) Deletion from the Beginning

Steps

  1. Remove element at index 0
  2. Shift remaining elements left

Time Complexity:
 O(n)

c) Deletion from a Given Index

Steps

  1. Remove element
  2. Shift remaining elements to fill the gap

Time Complexity:
 O(n)

5. Searching in Arrays

Searching means finding the index of a given element.

Used When

  • Array is unsorted

Steps

  • Compare each element with the target

Time Complexity

  • Best Case: O(1)
  • Worst Case: O(n)

Used When

  • Array is sorted

Steps

  • Divide the search space into halves repeatedly

Time Complexity

 O(log n)

Note

Binary search is much faster but requires sorted data.

6. Updating Elements in an Array

What Is Updating?

Updating means changing the value at a specific index.

Example

arr[3] = 50;

Time Complexity

O(1)

Why?

  • Direct address calculation
  • No shifting or traversal needed

7. Sorting Arrays

What Is Sorting?

Sorting rearranges array elements into a specific order (ascending or descending).

Common Sorting Algorithms

AlgorithmTime Complexity
Bubble SortO(n²)
Selection SortO(n²)
Insertion SortO(n²)
Merge SortO(n log n)
Quick SortO(n log n) (average)

Why Sorting Matters

  • Makes searching faster
  • Improves data organization
  • Required for binary search

                    

Common Sorting Algorithms and Time Complexity

AlgorithmBestAverageWorst
Bubble SortO(n)O(n²)O(n²)
Selection SortO(n²)O(n²)O(n²)
Insertion SortO(n)O(n²)O(n²)
Merge SortO(n log n)O(n log n)O(n log n)
Quick SortO(n log n)O(n log n)O(n²)
Heap SortO(n log n)O(n log n)O(n log n)

Sorting choice affects overall performance significantly.

8. Time Complexity Summary Table

OperationTime Complexity
AccessO(1)
TraverseO(n)
Insert at EndO(1)*
Insert at BeginningO(n)
Insert at IndexO(n)
Delete from EndO(1)
Delete from BeginningO(n)
Delete from IndexO(n)
Linear SearchO(n)
Binary SearchO(log n)
UpdateO(1)

* Amortized O(1) for dynamic arrays

9. Space Complexity of Arrays

  • Arrays require contiguous memory
  • Static arrays allocate memory once
  • Dynamic arrays may reallocate memory

Space Complexity:

  • O(n)

10. Why Insertions and Deletions Are Costly in Arrays

Because arrays store elements contiguously:

  • Removing or adding an element breaks the sequence
  • Shifting is required to maintain order

This limitation leads to:

  • Linked lists for frequent insertions/deletions
  • Dynamic arrays for flexible resizing

Relationship with Other Topics

This topic connects directly with:

  • Array Memory Representation
  • Advantages and Disadvantages of Arrays
  • Searching and Sorting Algorithms
  • Two Pointer and Sliding Window techniques

Next Topic:
Advantages and Disadvantages of Arrays

 

Sanjiv
0

You must logged in to post comments.

Related Blogs

Find the S...
Arrays February 02 ,2026

Find the Second Smal...

Array Memo...
Arrays December 12 ,2025

Array Memory Represe...

Advantages...
Arrays December 12 ,2025

Advantages and Disad...

Arrays in...
Arrays December 12 ,2025

Arrays in C

Vector in...
Arrays December 12 ,2025

Vector in C++ STL

Arrays vs...
Arrays December 12 ,2025

Arrays vs ArrayList...

JavaScript...
Arrays December 12 ,2025

JavaScript Arrays

Binary Sea...
Arrays December 12 ,2025

Binary Search on Arr...

Two Pointe...
Arrays January 01 ,2026

Two Pointers Techniq...

Prefix Sum...
Arrays January 01 ,2026

Prefix Sum Technique

Find the S...
Arrays January 01 ,2026

Find the Sum of All...

Find the M...
Arrays January 01 ,2026

Find the Maximum Ele...

Find the M...
Arrays January 01 ,2026

Find the Minimum Ele...

Count Even...
Arrays January 01 ,2026

Count Even and Odd N...

Search an...
Arrays January 01 ,2026

Search an Element in...

Copy One A...
Arrays January 01 ,2026

Copy One Array into...

Reverse an...
Arrays January 01 ,2026

Reverse an Array

Print Alte...
Arrays January 01 ,2026

Print Alternate Elem...

Find the L...
Arrays January 01 ,2026

Find the Length of a...

Check if a...
Arrays January 01 ,2026

Check if an Array is...

Find the F...
Arrays January 01 ,2026

Find the First Eleme...

Find the L...
Arrays January 01 ,2026

Find the Last Elemen...

Count the...
Arrays January 01 ,2026

Count the Number of...

Replace Al...
Arrays January 01 ,2026

Replace All Elements...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at E...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at O...

Find the A...
Arrays January 01 ,2026

Find the Average of...

Count the...
Arrays January 01 ,2026

Count the Number of...

Remove Dup...
Arrays January 01 ,2026

Remove Duplicate Ele...

Move All Z...
Arrays January 01 ,2026

Move All Zeros to th...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by K...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by O...

Check if T...
Arrays January 01 ,2026

Check if Two Arrays...

Merge Two...
Arrays January 01 ,2026

Merge Two Sorted Arr...

Find Missi...
Arrays January 01 ,2026

Find Missing Number...

Find Dupli...
Arrays January 01 ,2026

Find Duplicate Eleme...

Count Freq...
Arrays January 01 ,2026

Count Frequency of E...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All U...
Arrays January 01 ,2026

Find All Unique Elem...

Insert an...
Arrays January 01 ,2026

Insert an Element at...

Delete an...
Arrays January 01 ,2026

Delete an Element fr...

Find the I...
Arrays January 01 ,2026

Find the Index of an...

Find Union...
Arrays January 01 ,2026

Find Union of Two Ar...

Find Inter...
Arrays January 01 ,2026

Find Intersection of...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s...

Find the L...
Arrays January 01 ,2026

Find the Largest Sum...

Kadane’s A...
Arrays January 01 ,2026

Kadane’s Algorithm (...

Two Sum Pr...
Arrays January 01 ,2026

Two Sum Problem

Subarray w...
Arrays January 01 ,2026

Subarray with Given...

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Rearrange...
Arrays January 01 ,2026

Rearrange Array Alte...

Leaders in...
Arrays January 01 ,2026

Leaders in an Array

Equilibriu...
Arrays January 01 ,2026

Equilibrium Index of...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s,...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All P...
Arrays January 01 ,2026

Find All Pairs with...

Longest Co...
Arrays January 01 ,2026

Longest Consecutive...

Product of...
Arrays January 01 ,2026

Product of Array Exc...

Maximum Pr...
Arrays January 01 ,2026

Maximum Product Suba...

Find the F...
Arrays January 01 ,2026

Find the First Missi...

Count Inve...
Arrays January 01 ,2026

Count Inversions in...

Rearrange...
Arrays January 01 ,2026

Rearrange Array by S...

Check if A...
Arrays January 01 ,2026

Check if Array Can B...

Trapping R...
Arrays January 01 ,2026

Trapping Rain Water

Find Minim...
Arrays January 01 ,2026

Find Minimum in Rota...

Search in...
Arrays January 01 ,2026

Search in Rotated So...

Median of...
Arrays January 01 ,2026

Median of Two Sorted...

Merge Inte...
Arrays January 01 ,2026

Merge Intervals

Count Reve...
Arrays January 01 ,2026

Count Reverse Pairs

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Largest Re...
Arrays January 01 ,2026

Largest Rectangle in...

Maximum Su...
Arrays January 01 ,2026

Maximum Sum Rectangl...

Subarray S...
Arrays January 01 ,2026

Subarray Sum Equals...

Count Dist...
Arrays January 01 ,2026

Count Distinct Eleme...

Sliding Wi...
Arrays January 01 ,2026

Sliding Window Maxim...

Find K Max...
Arrays January 01 ,2026

Find K Maximum Eleme...

Minimum Nu...
Arrays January 01 ,2026

Minimum Number of Ju...

Chocolate...
Arrays January 01 ,2026

Chocolate Distributi...

Find All T...
Arrays January 01 ,2026

Find All Triplets Wi...

Kth Smalle...
Arrays January 01 ,2026

Kth Smallest Element...

Maximum Le...
Arrays January 01 ,2026

Maximum Length Biton...

Find the S...
Arrays February 02 ,2026

Find the Second Larg...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com