Data Structures and Algorithms — Arrays

Ahsan Majeed
4 min readJul 28, 2023

--

Imagine an array as a row of boxes, where each box can hold one item. You have a fixed number of boxes, and each box has a unique number starting from 0. You can put items in these boxes and easily find them later by their assigned numbers. Arrays are like organized storage units for data that make it convenient to access and manage multiple items.

Technical Explanation:

In computer science, an array is a data structure that stores a collection of elements of the same data type. Each element in the array is identified by its index, which is a numerical value indicating its position. Arrays have a fixed size, and the elements are stored in contiguous memory locations, making it easy to access them using their indices. Arrays are fundamental data structures and are widely used in programming for their efficiency in random access.

Types of Arrays:

1. Single-Dimensional Array: This is the basic type of array where elements are stored in a linear sequence.

2. Multi-Dimensional Array: This is an array of arrays, creating a matrix-like structure. For example, a 2D array can be visualized as a table with rows and columns.

Major Operations on Arrays (Java code and Complexity):

1. Single-Dimensional Array:

a. Accessing an element: You can access an element in the array by providing its index.

int[] arr = {10, 20, 30, 40};
int element = arr[2]; // Accesses the element with index 2 (30)

Complexity: O(1) — Constant time. Accessing an element directly using the index is fast as the position is known.

b. Insertion: You can insert an element at a specific index in the array.

int[] arr = {10, 20, 40};
int index = 2;
int element = 30;
int[] newArr = new int[arr.length + 1];

for (int i = 0, j = 0; i < newArr.length; i++) {
if (i == index) {
newArr[i] = element;
} else {
newArr[i] = arr[j];
j++;
}
}

Complexity: O(n) — Linear time. In the worst case, if you need to insert at the beginning of the array, all the existing elements need to be shifted to make space.

c. Deletion: You can remove an element from a specific index in the array.

int[] arr = {10, 20, 30, 40};
int index = 2;
int[] newArr = new int[arr.length - 1];

for (int i = 0, j = 0; i < arr.length; i++) {
if (i != index) {
newArr[j] = arr[i];
j++;
}
}

Complexity: O(n) — Linear time. Similar to insertion, if you remove an element from the beginning, all subsequent elements need to be shifted.

d. Search: You can search for an element in the array and get its index (if it exists).

int[] arr = {10, 20, 30, 40};
int searchElement = 30;
int index = -1;

for (int i = 0; i < arr.length; i++) {
if (arr[i] == searchElement) {
index = i;
break;
}
}


Complexity: O(n) — Linear time. In the worst case, you may need to traverse the entire array to find the element or determine it doesn’t exist.

2. Multi-Dimensional Array:

a. Accessing an element: You can access an element in the multi-dimensional array by providing its indices for each dimension.

int[][] matrix = { {1, 2}, {3, 4} };
int element = matrix[1][0]; // Accesses the element in the second row and first column (3)

Complexity: O(1) — Constant time. Accessing an element directly using the indices is fast as the position is known.

b. Insertion: Insertion in a multi-dimensional array requires specifying the indices for each dimension and the new element to be inserted.

int[][] matrix = { {1, 2}, {3, 4} };
int row = 1;
int column = 1;
int newElement = 5;
matrix[row][column] = newElement;


Complexity: O(1) — Constant time. As with single-dimensional arrays, inserting an element at a known position takes constant time.

c. Deletion: Deletion in a multi-dimensional array is similar to a single-dimensional array but requires specifying the indices for each dimension.

int[][] matrix = { {1, 2}, {3, 4} };
int row = 1;
int column = 0;
matrix[row][column] = 0; // Assuming 0 represents an empty element


Complexity: O(1) — Constant time. Deletion at a known position takes constant time.

Use Case:

Arrays are used in various applications, including but not limited to:

I- Storing and accessing a collection of elements with a fixed size, like temperatures of a week, scores of students, or RGB values of an image.
II- Implementing dynamic programming solutions, like in problems involving memoization or tabulation.
III- Representing grids, matrices, or tables in applications like image processing or gaming.
IV- Handling large datasets, particularly when random access to elements is required.

In conclusion, arrays are versatile data structures that provide a simple and efficient way to store and access collections of elements. They offer quick access to items using their indices and find extensive use in a wide range of programming applications. From managing lists of data to representing complex grids and matrices, arrays play a fundamental role in computer science. Understanding arrays is essential for any aspiring programmer, as they serve as a building block for more advanced data structures and algorithms. So, embrace the power of arrays and explore their capabilities to enhance your coding skills and problem-solving abilities. Happy coding!

--

--

Ahsan Majeed

My thoughts and world. Just want to keep sharing thoughts, experiments & new stuff.