Inside Data Structures and Algorithms: Arrays
By: Shubham Aggarwal
Tags: Algorithms, Programming, Data Structures Course, Arrays, Data Structures

I have always struggled with identifying why do we use certain data structure of algorithm to a certain problem ? Why are these the benchmarks ? Why do we just cram the concepts and later forget them ?Until I relate them to real world examples surrounding us and this eventually made my concepts clear. I no longer require to remember how each and every data structure and create concepts on my own. I am creating this series to publish such stories that helped me in understanding what is known as Data Structures & Algorithms.

Arrays

We have all played or familiar with a pool game. No wonder 8 ball pool has more than 18M downloads!! There are a lot of 16 balls in a pool game, 15 with numbers from 1–15 and 1 white ball. These balls generally comes in a ball box having capacity to put 16 balls in it.

Something like this.

Let’s say each hole in the box has a number written on it telling us which ball should be placed at this hole (Consider white as 16). After you are done playing a pool game, a really good guy comes and decided to put these balls back into the box (lets admit you are not going to).

How will he put the balls back ?

He picks up every ball one by one from the pool table.

Reads the number on the ball.

Reads the number in the box.

Puts the ball to a corresponding numbered hole. (he is a really nice person)

Phew!! That was quite a task. Let’s do a retrospective on what we saw above.

Pool box has a capacity of 16 balls. Let’s call this `n`.

Each consecutive number is written one after the other continuously. For simplicity, consider all the holes to be in a straight line.

The logic of putting the balls back into box is mapped by same number as written on the box. Lets’ define this mapping fn as map(x). Here, map(x) = x for x ≤16

We can put less or equal number of balls to box’s capacity. Though box still has 16 holes in it.

These real world scenarios’ above made computer scientists think how do we make computer do the same think, what our mind does without thinking ? Thus, building blocks for making a computer robot. Scarryy!!

They defined something called as Arrays which is collection of items stored at contiguous memory locations. The idea is to store multiple items of same type together.

We reserve a contiguous memory location of capacity n, and assign this memory location to a variable in a programming language. If we don’t assign this to a variable, this reserved memory location is lost for our program.

We then identified a lot of similar type of real world structures around us, and dumped them into a family of DataStructures. Now, Array has a happy family. Finally!!

We will discuss other DataStructures in next series.

Beware: too much technical details below!!

What do we mean by contiguous memory locations ?

To understand this, first we need to look at what memory is in a computer world.

Our computer uses RAM to perform basic functions. Each of these comes with a specific size like 4 GB, 8 GB, 256 GB etc. These number quantify the memory a computer system have.

What is the basic unit of memory that above sizes deal with ?

Ex: 4 Kilo-bytes, 8 Giga-bytes. A byte is comprised of 8 bits (similar to array). The basic unit of memory is a bit.

We, as humans, deal with number from 0–9, also known as decimal numbers (deci — 10 numbers). Every other number is a combination of these numbers. On the other hand, computer deals with numbers from 0–1, also known as binary numbers (bi— 2 numbers).

So, each bit can contain either 0 or 1. This means, a byte of 8 bits can contain anything from 00000000 to 11111111. Converting binary to decimal it translates to 0 to 255. (Read about binary to decimal conversions). There are total of 2⁸ combinations for a byte. (either 0 or 1 at any place).

So, if you want to store an array of integers. How much memory do you need ?

In computer systems, the range of integers is specified from -2,147,483,648 to 2,147,483,647. The total numbers in this range comes out to be 42,949,672,96 which is 2³² .

2³² = 2⁸ (1byte)* 2⁸ (1byte)* 2⁸ (1byte) * 2⁸ (1byte)

An integer takes a memory capacity of 4 bytes to be stored. When we define an integer, we reserve 4 bytes of memory storage to store any possible integer value.

Coming back to our problem of what do we mean by memory locations, remember we talked about how a byte is a consecutive set of 8 bits in memory. Consider a set of bytes to be a continuous set of memory locations.

Imagine whole RAM memory as a continuous set of bytes (for simplicity). When we define an array with a capacity n, our computer program reserves n blocks of 4-byte memory storage. Recall that all of these blocks are continuous memory locations.

Activity: Try to write a program which allocates an array memory equal to the RAM size of your computer.