1.2 Array
1.2 Array
1.2 Array
Introduction to Arrays
An array is collection of items stored at contiguous memory
locations. The idea is to store multiple items of same type
together. This makes it easier to calculate the position of
each element by simply adding an offset to a base value,
i.e., the memory location of the first element of the array
(generally denoted by the name of the array).
For simplicity, we can think of an array a fleet of stairs
where on each step is placed a value (let’s say one of your
friends). Here, you can identify the location of any of your
friends by simply knowing the count of the step they are on.
Remember: “Location of next index depends on the data
type we use”.
Arrays can be declared in various ways in different languages. For illustration, let's
take C array declaration.
As per the above illustration, following are the important points to be considered.
1. Index starts with 0.
2. Array length is 10 which means it can store 10 elements.
3. Each element can be accessed via its index. For ex, we can fetch an element at index
6 as 27.
Calculation of address
The address of any particular element in the 1D array can be
calculated by the following equation,
Address of element a[k] = B+W*k
Here, B is the base address of the array, W is the size of each
element of the array, and the number of elements needed in the
array is k (i.e. index of element).
Example
For example, we wish to find the address of a 6th element of the
one dimensional array ‘a[10]’, whose base address is 1000. The
size of each element in this example is 4 byte. So, the calculation
based on this can be performed in the following way.
Address of element a[6] = 1000+4*6
= 1000+24
= 1024
Basic Operations
Algorithm
Consider LA is a linear array with N elements and K is a positive
integer such that K<=N. Following is the algorithm to delete an
element available at the K th position of LA.
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Deleting the first element of the array
Advantages
1. Reading an array element is simple and efficient. As
shown in the above table, the read time of array is O(1) in
both best and worst cases. This is because any element can
be instantly read using indexes (base address calculation
behind the scene) without traversing the whole array.
2. Array is a foundation of other data structures. For example
other data structures such as LinkedList, Stack, Queue etc.
are implemented using array.
3. All the elements of an array can be accessed using a single
name (array name) along with the index, which is readable,
user-friendly and efficient rather than storing those elements
in different-2 variables.
Disadvantages
1. While using array, we must need to make the decision of
the size of the array in the beginning, so if we are not
aware how many elements we are going to store in array, it
would make the task difficult.
2. The size of the array is fixed so if at later point, if we
need to store more elements in it then it can’t be done. On
the other hand, if we store less number of elements than
the declared size, the remaining allocated memory is
wasted.
Two dimensional (2D) arrays
An array of arrays is known as 2D array. The two
dimensional (2D) array is also known as matrix. A matrix
can be represented as a table of rows and columns.
A two-dimensional array is stored in the form of the row-
column matrix, where the first index designates the row and
second index shows the column. The second or the rightmost
index of an array alters very fastly as compared to the first or
left-most index while accessing the elements of an array.
These matrices are stored in the memory as given below.
Row-Major order Implementation
Column-Major order Implementation
Row-Major order Implementation