We launched our first course, Introduction to Data Structures in C++! Learn more 🎉

Data Structures

Operations on Arrays

July 10, 2022

An array is basically a collection of items that belong to the same data type. Formally, it is a linear data structure which stores data items of the same type. If you want to learn more about what arrays are and their fundamentals, you can check this post out.


Once you have stored data in an array, you can perform any operation on it like sorting, searching, traversal, etc. on it. But today we will be talking about some of the basic operations that can be performed on arrays because they are the fundamentals. Below are the basics operations that we will be learning about:


1. Traversal

2. Insertion

3. Deletion

4. Searching




Operation 1: Traversal

What does traversal exactly mean?

Let’s say, you’re sitting in an exam hall with other students and you’re waiting for your professor to hand you the question paper 📝 Assume you’re sitting on the 3rd bench in the 1st row. Your professor starts distributing the question paper by going to every student’s desk exactly once. This operation is called traversal 🎉

Formally, traversal refers to visiting every element of an array exactly once. Sounds simple right?

This simple operation can be used for multiple purposes like printing elements of an array, initialising an array, etc.


How can I traverse an array in C++ though?

There are multiple ways in which you can do this, but I’ll talk about how you can traverse an array using for loop.

Say you have already initialised an array of size 5. Here’s how you can traverse it and print all the elements in the array:

What according to you are the other ways in which we can traverse an array? Send me your ideas at hello@byteavenue.org or DM me on Instagram!



Operation 2: Insertion

Let’s first talk about what does insertion operation even mean. Say you have a big house with 10 rooms in it. Out of this only the first four rooms are used 🏡 A guest comes at your place and occupies the fifth room. This operation is similar to insertion in an array.

Formally, insertion is inserting an element at its respective position in an array.


How can I insert an element in an array in C++?

An element in array can either be added at the beginning of the array, end of the array, or somewhere between the first and last array elements right?

Thus, there are multiple cases in insertion. We will only see an example of how to add an element at the end of an array here.

Say you have an array of size 6 and there are 5 elements in the first 5 positions. You have to add an element at the end of the array. Here’s how you’ll do it in C++:



For inserting either at the beginning of an array or at any other index in the array, you’ll have to shift all the other elements to make space for the new element and then insert the new element in that position. You can try this out and send me your implementation at hello@byteavenue.org or DM me on Instagram! You can also drop a message at any of these if you face any issues.



Operation 3: Deletion

Deletion is just the opposite of insertion. It refers to removing an existing element from the array and re-organizing all elements of an array.

Similar to insertion, there will be multiple cases here too.

1. Deletion at the beginning of an array.

2. Deletion at the end of an array.

3. Deletion at a given index of an array.


But the approach to delete an element in all the cases will be same. You just replace the element you want to delete with the element in front of it and keep doing the same until the end of the array. Instead of me giving the code, I recommend you trying this one out too and sharing the code with me on any of the above channels.



Operation 4: Searching

As we know, searching refers to finding a particular element in the array.

There are again multiple ways in which you can search for an element in an array. This is where the searching algorithms come in. We have linear search, binary search, jump search, and so on, but you don’t need to worry about them yet. We’ll learn about them later 😌

The most simple way in which you can search for an element in an array is by traversing through the array and seeing if the element that you’re looking for exists in that position or not. Here’s a function that can help you do this.

🛑 Don’t look at this directly 🛑 Try by yourself first and come back if you can’t figure it out.



These are the basic operations that you can perform on an array. There are other operations too that you can perform on an array but these are enough to get started with 🥳


Hope this is helpful and gives you a basic idea on what space complexity is and how it is calculated 😌 You can send me a mail at hello@byteavenue.org or DM me on Instagram if you have any questions 💪


Until next time 👋

Want to learn more?

Our first course, Introduction to Data Structures in C++ worth ₹499/month is available at a price of your choice for the first month access.

Offer available for a limited time

Subscribe

Learn more

301/302, 3rd Floor, Saket Callipolis, Sarjapur Main Rd, Doddakannelli, Bengaluru, Karnataka 560035

© CodePark Pvt. Ltd. 2022

301/302, 3rd Floor, Saket Callipolis, Sarjapur Main Rd, Doddakannelli, Bengaluru, Karnataka 560035

© CodePark Pvt. Ltd. 2022

301/302, 3rd Floor, Saket Callipolis, Sarjapur Main Rd, Doddakannelli, Bengaluru, Karnataka 560035

© CodePark Pvt. Ltd. 2022