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:
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.
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.
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.
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.
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:
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.
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.
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.
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.
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.
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)
Last Updated :
03 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...