In this article, we will discuss array data structure, how to initialize it and how to work on it. We will deep dive and discuss what is an array and how to use it. Array is the most used data structure as it is capable of storing multiple data of the same type in the programming field.
What is an Array Data Structure?
A linear data structure called an array contains elements of the same data type in contiguous and nearby memory regions. Arrays operate using an index system with values ranging from 0 to (n-1), where n is the array’s size.
Although it is an array, arrays were introduced for a reason.
Why Do You Need Array Data Structure?
Consider a class of ten students that is required to report its results. It would be difficult to manipulate and preserve the data if you had specified each of the ten variables individually.
It would be more challenging to declare and maintain track of all the variables if more students joined. Arrays were introduced as a solution to this issue.
What Are the Types of Arrays?
There are primarily three types of arrays:
One-Dimensional Arrays:
A one-dimensional array can be thought of as a row where elements are kept one after the other.
Multi-Dimensional Arrays:
There are two different types of these multidimensional arrays. Those are:
Two-Dimensional Arrays:
It can be compared to a table with elements in each cell.
Three-Dimensional Arrays:
It is comparable to a larger cuboid composed of smaller cuboids, where each cuboid can hold a different element.
One-dimensional arrays will be used in this session on "arrays data structure."
How Do You Declare an Array?
The size of the arrays is often the argument in square bracket definitions for arrays.
The syntax for arrays is as follows:
- 1D Arrays: int arr[n];
- 2D Arrays: int arr[m][n];
- 3D Arrays: int arr[m][n][o];
How Do You Initialize an Array?
An array can be initialized in four different ways.:
Method 1:
int a[6] = {2, 3, 5, 7, 11, 13};
Method 2:
int arr[]= {2, 3, 5, 7, 11};
Method 3:
int n;
scanf(“%d”,&n);
int arr[n];
for(int i=0;i< 5;i++)
{
scanf(“%d”,&arr[i]);
}
Method 4:
int arr[5];
arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;
arr[4]=5;
How Can You Access Elements of Arrays in Data Structures?
The index where you stored the element can be used to access it. Let's talk about it using a code:
#include<stdio.h> int main() { int a[5] = {2, 3, 5, 7, 11}; printf("%d\n",a[0]); // we are accessing printf("%d\n",a[1]); printf("%d\n",a[2]); printf("%d\n",a[3]); printf("%d",a[4]); return 0; }
Output
2
3
5
7
11
Basic operations of array data structure
Let's now talk about the basic operations that the array data structure supports. –
- Traversal – The array's elements are printed using this operation.
- Insertion – It's used to add an element to a specific index.
- Deletion – It is used to remove an element from a specific index.
- Search – It is used to search for an element using either the value or the specified index.
- Update – This operation updates an element at a specific index.
Traversal operation
The array elements are traversed using this operation. It sequentially prints each element of the array. The following program will help us to comprehend it. –
#include <stdio.h> void main() { int Arr[5] = {18, 30, 15, 70, 12}; int i; printf("Elements of the array are:\n"); for(i = 0; i<5; i++) { printf("Arr[%d] = %d, ", i, Arr[i]); } }
Output
Elements of the array are:
Arr[0] = 18, Arr[1] = 30, Arr[2] = 15, Arr[3] = 70, Arr[4] = 12,
Insertion operation
One or more members are added to the array by using this method. An element can be added to the array at any index, at the beginning or end, or both, depending on the specifications. Let's see the implementation of adding an element to the array right away.
#include <stdio.h> int main() { int arr[20] = { 18, 30, 15, 70, 12 }; int i, x, pos, n = 5; printf("Array elements before insertion\n"); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); x = 50; // element to be inserted pos = 4; n++; for (i = n-1; i >= pos; i--) arr[i] = arr[i - 1]; arr[pos - 1] = x; printf("Array elements after insertion\n"); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); return 0; }
Output
Array elements before insertion
18 30 15 70 12
Array elements after insertion
18 30 15 50 70 12
Deletion operation
As the name suggests, this operation rearranges every element in the array after removing one element from it.
#include <stdio.h> void main() { int arr[] = {18, 30, 15, 70, 12}; int k = 30, n = 5; int i, j; printf("Given array elements are :\n"); for(i = 0; i<n; i++) { printf("arr[%d] = %d, ", i, arr[i]); } j = k; while( j < n) { arr[j-1] = arr[j]; j = j + 1; } n = n -1; printf("\nElements of array after deletion:\n"); for(i = 0; i<n; i++) { printf("arr[%d] = %d, ", i, arr[i]); } }
Output
Given array elements are :
arr[0] = 18, arr[1] = 30, arr[2] = 15, arr[3] = 70, arr[4] = 12,
Elements of array after deletion:
arr[0] = 18, arr[1] = 30, arr[2] = 15, arr[3] = 70,
Search operation
Using the value or index, this operation is used to search for an element in the array.
#include <stdio.h> void main() { int arr[5] = {18, 30, 15, 70, 12}; int item = 70, i, j=0 ; printf("Given array elements are :\n"); for(i = 0; i<5; i++) { printf("arr[%d] = %d, ", i, arr[i]); } printf("\nElement to be searched = %d", item); while( j < 5){ if( arr[j] == item ) { break; } j = j + 1; } printf("\nElement %d is found at %d position", item, j+1); }
Output
Given array elements are :
arr[0] = 18, arr[1] = 30, arr[2] = 15, arr[3] = 70, arr[4] = 12,
Element to be searched = 70
Element 70 is found at 4 position
Update operation
This action is used to update an array element that is already present and is located at the specified index.
#include <stdio.h> void main() { int arr[5] = {18, 30, 15, 70, 12}; int item = 50, i, pos = 3; printf("Given array elements are :\n"); for(i = 0; i<5; i++) { printf("arr[%d] = %d, ", i, arr[i]); } arr[pos-1] = item; printf("\nArray elements after updation :\n"); for(i = 0; i<5; i++) { printf("arr[%d] = %d, ", i, arr[i]); } }
Output
Given array elements are :
arr[0] = 18, arr[1] = 30, arr[2] = 15, arr[3] = 70, arr[4] = 12,
Array elements after updation :
arr[0] = 18, arr[1] = 30, arr[2] = 50, arr[3] = 70, arr[4] = 12,
Complexity of Array operations
The following table lists the time and space complexity of several array operations..
Time Complexity
Operation | Average Case | Worst Case |
---|---|---|
Access | O(1) | O(1) |
Search | O(n) | O(n) |
Insertion | O(n) | O(n) |
Deletion | O(n) | O(n) |
Space Complexity
The worst possible space complexity of an array is O(n)..
Advantages of Array
- The group of identically named variables is referred to as an array. As a result, it is simple to recall the names of all the array's elements.
- It is relatively easy to navigate an array; all we need to do is increase the array's base address to visit each element one at a time.
- The index can be used to directly access any element in the array.
Disadvantages of Array
- The array is uniform. This implies that it can store elements with similar data types.
- An array has static memory allocation, meaning that its size cannot be changed.
- If we store less elements than the given size, memory will be wasted.
Conclusion
The array data structure and the fundamental operations on it were covered in this article. Arrays offer a special technique to store the data that is being kept so that it can be quickly accessible and queried to retrieve the value using the index.