Horje
Time and Space complexity of 1-D and 2-D Array Operations

The time and space complexity of one-dimensional and two-dimensional array operations can vary depending on the specific operation. Here, we’ll discuss common array operations and provide insights into their time and space complexities for one-dimensional and two-dimensional arrays.

One-Dimensional Array Operations:

  1. Accessing an Element by Index:
    • Time Complexity: O(1)
    • Space Complexity: O(1)
    • Accessing an element in a one-dimensional array by its index is typically a constant-time operation because it directly computes the memory location of the element.
  2. Inserting an Element at the End:
    • Time Complexity: O(1) (Amortized)
    • Space Complexity: O(1)
    • Inserting an element at the end of a one-dimensional array usually involves updating the array’s size and placing the new element in the next available position, which is a constant-time operation on average.
  3. Inserting an Element at the Beginning:
    • Time Complexity: O(n)
    • Space Complexity: O(n)
    • Inserting an element at the beginning of a one-dimensional array requires shifting all existing elements to make room, resulting in a linear time and space complexity.
  4. Searching for an Element (Linear Search):
    • Time Complexity: O(n)
    • Space Complexity: O(1)
    • In the worst case, searching for an element in a one-dimensional array may require looking at every element, resulting in a linear time complexity. The space complexity remains constant.
  5. Deleting an Element:
    • Time Complexity: O(n)
    • space complexity: O(1)
    • In the worst case, deleting an element from the front may take O(n) time as elements after an element should be shifted by one position.

Two-Dimensional Array Operations:

  1. Accessing an Element by Indices:
    • Time Complexity: O(1)
    • Space Complexity: O(1)
    • Accessing an element in a two-dimensional array using row and column indices is generally a constant-time operation, similar to one-dimensional arrays.
  2. Inserting an Element at a Specific Position:
    • Time Complexity: O(1)
    • Space Complexity: O(1)
    • Inserting an element at a specific position in a two-dimensional array typically has a constant-time complexity because it directly computes the memory location of the element based on its indices.
  3. Searching for an Element (Linear Search):
    • Time Complexity: O(m * n)
    • Space Complexity: O(1)
    • When searching for an element in a two-dimensional array, you may need to examine all elements in the worst case, resulting in a time complexity of O(m * n), where ‘m’ is the number of rows and ‘n’ is the number of columns. The space complexity remains constant.
  4. Deleting an Element:
    • Time complexity: O(m*n)
    • Space complexity: O(1)
    • Deleting an element from a 2D array requires shifting of elements after deletion operation.
  5. Transposing a Matrix:
    • Time Complexity: O(m * n)
    • Space Complexity: O(m * n)
    • Transposing a two-dimensional array involves swapping elements across the diagonal. This operation requires examining and potentially swapping all elements, resulting in a time complexity of O(m * n) and a space complexity of O(m * n).

Below is a table summarizing the time and space complexities of various operations on one-dimensional and two-dimensional arrays.

Operation One-Dimensional Array Two-Dimensional Array
Accessing an Element by Index Time: O(1) Time: O(1)
Space: O(1) Space: O(1)
Inserting an Element at the End Time: O(1) (Amortized) Time: O(1)
Space: O(1) Space: O(1)
Inserting an Element at the Beginning Time: O(n) Time: O(1)
Space: O(n) Space: O(n)
Searching for an Element (Linear Search) Time: O(n) Time: O(m * n)
Space: O(1) Space: O(1)
Transposing a Matrix N/A Time: O(m * n)
N/A Space: O(m * n)

Deletion of an Element

Time:O(n)

Time:O(m*n)

Space:O(1)

Space:(1)




Reffered: https://www.geeksforgeeks.org


Arrays

Related
What is a Permutation Array in DSA? What is a Permutation Array in DSA?
Find the number of Enjoyable Chocolates for Geek's Budget Find the number of Enjoyable Chocolates for Geek's Budget
Find Cumulative Sum Of a List, Vector or Array Find Cumulative Sum Of a List, Vector or Array
Count Pairs with equal elements and Divisible Index Sum Count Pairs with equal elements and Divisible Index Sum
Find the Target number in an Array Find the Target number in an Array

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
10