1.2 Array
1.2 Array
Syllabus
Arrays: Definition, Single and Multidimensional Arrays,
Representation of Arrays: Row Major Order, and Column
Major Order, Derivation of Index Formulae for 1-D,2-D,3-
D and n-D Array Application of arrays, Sparse Matrices
and their representations.
Introduction to Arrays
The simplest and linear type of data structure is a linear (or
1D) array.
One way is to have linear relationship between elements by
means of sequential (contiguous) memory locations.
An array is collection of items stored at contiguous memory
locations. The idea is to store multiple items of same type
(homogenous) together.
It easier to calculate the location of each element by simply
adding an offset to a base value, i.e., the memory location of
the first element of the array.
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 1-D array can be
calculated by the following equation:
Address of element a[k] = Base address + W*E
Here, W is the size of each element of the array, and E is the
effective index (E=k-LB) of element we need in the array.
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
Following are the basic operations supported by an array.
Traverse − print all the array elements one by one.
Insertion − Adds an element at the given index.
Deletion − Deletes an element at the given index.
Search − Searches an element using the given index or by
the value.
Update − Updates an element at the given index.
Traversal of Array
The method of processing each element in the array exactly
once is known as Traversal. In array Traversal starts from
first element in the array and ends at the last element of the
array.
Traversing an array means accessing each and every element
of the array for a specific purpose. For ex. printing every
element, counting the total number of elements, or
performing any process on these elements.
Time complexity of traversal algorithm is O(n) in all cases.
Algorithm for Array Traversal
Step 1: START = 0
Step 2: Repeat Step-3 while (START<N)
Step 3: Read A[START]
START = START + 1
Program for Array Traversal
#include<stdio.h>
#define N 5
void traverse(int *A);
int main()
{
int A[N]={10,20,30,40,50};
traverse(A);
}
void traverse(int *A)
{
int START=0;
while(START<N)
{
printf("%d\n“, A[START]);
START=START+1;
}
}
Insertion in an Array
Following can be a situation with array insertion:
Insertion at the beginning of an array
Insertion at the given index of an array
Insertion after/before the given index of an array
Insertion at the Beginning of an Array
When the insertion happens at the beginning, it causes all the
existing data items to shift one step towards right.
We assume A is an array with N elements. The maximum numbers
of elements it can store is defined by MAX. We shall first check if
an array has any empty space to store any element and then we
proceed with the insertion process. The time complexity is O(n).
Algorithm
1. Begin
2. IF N == MAX
return
3. ELSE N = N + 1
a. For all Elements in A Move to next adjacent location
b. A[FIRST] = New Element
4. End
Insertion at the given index of an array
In this scenario, we are given the exact location (index) of an array where a
new data element (value) needs to be inserted. First we shall check if the array
is full, if it is not, then we shall move all data elements from that location to
one step towards right. This will make room for a new data element.
We assume A is an array with N elements. The maximum numbers of elements
it can store is defined by MAX.
Algorithm
1. Begin
2. IF N == MAX
return
3. ELSE N = N + 1
a. Input index
b. For All Elements from A[index] to A[N], Move to next adjacent
location
c. A[index] = New Element
4. End
Insertion after the given index of an array
In this scenario we are given a location (index) of an array after which a
new data element (value) has to be inserted. Only the seek process varies,
the rest of the activities are the same as in the previous example.
We assume A is an array with N elements. The maximum numbers of
elements it can store is defined by MAX.
Algorithm
1. Begin
2. IF N = MAX
return
3. ELSE
N=N+1
a. Input index
b. For All Elements from A[index + 1] to A[N] Move to next adjacent
location
c. A[index + 1] = New Element
3. End
The time complexity of insertion algorithm is:
Best case: Ω(1)
Worst Case: O(n)
Average Case: θ(n)
Deletion in Array
Deletion refers to removing an existing element from the
array and re-organizing all elements of an array.
Deleting the first element of the array
Delete an element
GATE-CS-2015 (Set 2)
An unordered array contains n distinct elements. The number of
comparisons to find an element in this list that is neither
maximum nor minimum is:
Θ(1)
Θ(nlogn)
Θ(n)
Θ(logn)
n(n-1)/4
GATE CS 2005
A program P reads in 500 integers in the range
[0..100] representing the scores of 500 students. It then
prints the frequency of each score above 50. What would
be the best way for P to store the frequencies?
An array of 50 numbers
An array of 100 numbers
An array of 500 numbers
A dynamically allocated array of 550 numbers
560
GATE-CS-2014-(Set-3)
Let A be a square matrix of size n x n. What is the expected output of the following program:
C = 100
for i = 1 to n do
for j = 1 to n do
{
Temp = A[i][j] + C
A[i][j] = A[j][i]
A[j][i] = Temp - C
}
for i = 1 to n do
for j = 1 to n do
Output(A[i][j]);