Summary

An array stores items in a row, one after another in memory. Each item has an index, starting at 0. Because the items sit next to each other, reading any one by its index is instant. The catch is that inserting or removing in the middle is slow, since everything after it must shift.

Arrays are the first data structure you should learn. Almost everything else in DSA is built on top of them. Get arrays right and the rest comes easier.

Most tutorials tell you that reading an array by its index is instant. Few tell you why. That why is the whole point, so let me show you what is really happening in memory.

What is an array in data structure?

An array is a collection of items stored in a row. Each item sits in its own slot, and the slots are numbered. That number is called the index, and it starts at 0, not 1.

So an array of five numbers has indexes 0, 1, 2, 3, and 4. The first item is at index 0. The last is at index 4. This off by one start trips up almost every beginner, so it is worth fixing in your head early.

The items in an array are usually all the same type, like all numbers or all words. This sameness is part of why arrays are so fast, as you are about to see. Arrays are the cornerstone of any DSA roadmap.

Why is reading by index instant?

Here is the part most tutorials skip. When you write arr[3], the computer does not search for the fourth item. It calculates exactly where that item lives, in one step.

An array sits in one solid block of memory. The computer knows the address where the array starts, called the base. It also knows how many bytes each item takes. So to find any item, it does simple math. The address is the base, plus the index times the size of one item.

For arr[3], it takes the start address and jumps forward three item sizes. No scanning, no looping, no matter how big the array is. That one calculation is why reading by index is O(1), which means constant time. This is the array’s superpower.

The trade-off no one mentions

That same solid block of memory has a cost. Because the items are packed tightly with no gaps, you cannot just drop a new item into the middle. There is no room.

Say you want to insert a value at the front of an array with a million items. Every single one of those items has to shift one slot to the right to make space. That is a million moves for one insert. The same goes for deleting from the middle, since everything after the gap must shift left to close it.

The part most tutorials skip

An array is fast to read and slow to rearrange. Reading any item is O(1), but inserting or deleting in the middle is O(n), because of all the shifting. This single trade-off is the reason other structures exist. Linked lists give up instant reads to gain fast inserts. Knowing this trade-off is what lets you pick the right structure instead of always reaching for an array.

Array code in Python and C++

In Python, the built in list works as an array. Here are the basic moves, reading, changing, and adding an item.

arr = [4, 8, 15, 16, 23]
print(arr[2])        # read index 2, prints 15
arr[2] = 42          # change index 2
arr.append(99)       # add to the end, fast
print(len(arr))      # how many items, prints 6

Reading and changing by index are the instant operations. Adding to the end is usually fast too. Here is the same idea in C++, the language most Indian placement tests expect.

int arr[5] = {4, 8, 15, 16, 23};
int x = arr[2];      // read index 2, x is 15
arr[2] = 42;         // change index 2

for (int i = 0; i < 5; i++) {
    cout << arr[i] << " ";   // print each item
}

In C++ a basic array has a fixed size set when you create it. Python lists grow on their own, which is handier but hides some of the cost. Both use the same index math underneath.

What is the time complexity of array operations?

Here is the full picture in plain terms. Reading or changing an item by its index is O(1), instant, thanks to the address math. Adding to the very end is usually O(1) as well.

The slow operations are inserting or deleting anywhere but the end, which are O(n) because of the shifting. Searching an unsorted array is also O(n), since you may have to check every item with a linear search. For space, an array of n items uses O(n) memory, which is as lean as it gets. You can read the formal definition on Wikipedia’s array page.

Where are arrays used?

Arrays are everywhere, because the row of slots maps to so many real things. A list of high scores in a game is an array. The pixels in an image are stored in arrays. Your music playlist is an array of songs.

They also power other algorithms. Sorting methods like selection sort work directly on arrays, rearranging items by index. Almost any time you have an ordered list of things to hold, an array is the first tool to reach for, as long as you are not inserting in the middle a lot.

FAQ

Why do array indexes start at 0?

Because the index is really a distance from the start. The first item is zero steps from the base address, so its index is 0. This makes the address math clean and fast.

What is the time complexity of reading an array element?

O(1), or constant time. The computer calculates the exact address from the index in one step, so it does not matter how large the array is.

Why is inserting into an array slow?

Because items are packed tightly with no gaps. To insert in the middle, every item after that spot must shift over to make room, which takes O(n) time.

What is the difference between an array and a list in Python?

In everyday Python, the built in list acts as a flexible array that can grow and hold mixed types. A true fixed type array needs a separate module, but for learning, a list is fine.

Can an array change size after it is created?

A basic array in languages like C++ has a fixed size. Python lists resize automatically by quietly making a bigger array and copying items over when needed.

When should I not use an array?

Avoid arrays when you insert or delete in the middle often, since that shifting is slow. A linked list handles frequent inserts better, trading away the instant index reads.

So what should you remember?

An array is a row of numbered slots in one block of memory. That layout makes reading any item instant, using simple address math, which is the whole reason arrays are so popular.

The price is slow inserts and deletes in the middle. Hold both facts together and you understand not just arrays, but why every other data structure exists to make a different trade.

Now try it. In the array [10, 20, 30, 40, 50], what value is at index 3, and how many items must shift if you insert a new value at index 0?