There can be many possible operations on arrays. in data structures, For example, searching, sorting, traversing odd elements, etc. But, we will discuss some of the standard operations on Arrays in data structures. All other operations are further derived from these.

Operations on Arrays –
- Traversal
- Insertion
- Deletion
- Searching
- Sorting
Traversal
Traversal in an array means visiting each element of the array exactly once. This can be done using a loop that will run from the starting index to the last of the array.
Coding example –
#include<iostream> using namespace std; int main(){ int arr[10] = {1,11,2,22,3,33,4,44,5,55}; for(int i=0;i<10;i++) cout<<arr[i]<<endl; //traversal }
Output –
1 11 2 22 3 33 4 5 55
Insertion –
This means inserting an element at any specific position. We need to give the index of the location at which we want to insert the element.
Coding Example –
#include<iostream> using namespace std; int main(){ int arr[5]; arr[0] = 4; //Insertion arr[1] = 5; cout<<arr[1]<<endl; }
Output –
5
Note: If we insert an element at a position that already contains a previous element so, the previous element is replaced now with the latest value.
Coding example –
#include<iostream> using namespace std; int main(){ int arr[5]; arr[0] = 4; //Insertion arr[1] = 5; arr[0] = 9; // re-insertion cout<<arr[0]<<endl;
Output –
9
Explanation –
In this case, the latest value will be printed, i.e. 9, not the value that was previously inserted i.e., 4.
Deletion –
We cannot delete a specific element directly, but we will need a set of operations to do this. The set of operations depends upon whether we need to maintain the previous order or not.
We will understand this with the help of examples –
#include<iostream> using namespace std; int main(){ int size = 10; int arr[size]; //delete 3rd element for(int i=0;i<size;i++){ arr[i] = i+1; } for(int i=2;i<size;i++){ arr[i] = arr[i+1]; } size--; for(int i=0;i<size;i++){ cout<<arr[i]<<endl; } }for(int i=0;i<4;i++){ cout<<arr[i]<<” ”<<endl; } }
Output –
1 2 4 5 6 7 8 9 10
Explanation-
In an array, we cannot delete an element directly. We need to push all the elements after it to just the previous location. This will ensure the deletion of the desired element.
This deletion is much simpler if we don’t want the order of the elements after deletion to be exactly the same.
Coding Example –
#include<iostream> using namespace std; int main(){ int size = 10; int arr[size]; //delete 3rd element for(int i=0;i<size;i++){ arr[i] = i+1; } arr[2] = arr[size-1]; size--; for(int i=0;i<size;i++){ cout<<arr[i]<<endl; } }
In this, we can copy the last element at that location and then reduce the size by 1.
Searching –
There are two different methods to search an element in a given array. The first one is sequential search in which we traverse the whole array and if the element is found, the index is the desired index. This method of searching is quite complex and time-consuming because we need to traverse the whole array if the element is found at the last location.
So, a different searching technique is used quite often which is called binary search. This can be used only if the array is sorted, or if the array is unsorted, we need to sort the array first.
In this article, we will learn about sequential search because binary search requires some more operations which we will learn in upcoming articles.
Coding example for sequential search –
#include<iostream> using namespace std; int main(){ int arr[10]; for(int i=0;i<10;i++){ arr[i] = i+1; } for(int i=0;i<10;i++){ if(arr[i] == 7) cout<<"Element found at index "<<i<<endl; } If(i == 10) cout<<”Element not found”<<endl’ }
Output –
An element found at index 6
Explanation –
We traversed at every index and compared each element to the value that we want to search. If we reach the end of the array and no element matches the value that we want to search, that means the given array does not contain that value.
Sorting –
Sorting an array means ordering the elements in an increasing or decreasing order. Sorting is done to see the elements in a systematic way or make further operations easier.
There are many techniques to sort the elements of an array. Here, we will learn a basic sorting technique which is called bubble sort and in further articles, we will learn about other sorting techniques.
#include <iostream> using namespace std; int main() { int data[] = {-2, 45, 0, 11, -9}; int size = sizeof(data) / sizeof(data[0]); for (int step = 0; step < size; ++step) { for (int i = 0; i < size - step; ++i) { if (array[i] > array[i + 1]) { int temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; } } } cout << "Sorted Array in Ascending Order:\n"; for (int i = 0; i < size; ++i) { cout << " " << array[i]; } cout << "\n"; }
Output –
-9 -2 0 0 11
So, in This article, we learned about some of the operations that are performed in an Array. We will learn more about searching and sorting specific articles.