1.2 Array
1.2 Array
Introduction to Arrays
The simplest type of data structure is a linear (or 1-Dimensional)
array.
One way is to have linear relationship between elements by
means of sequential memory locations and other way is to
have a linear relationship between elements by means of links
(relationship of adjacency is maintained by link).
An array is collection of items stored at contiguous memory
locations. The idea is to store multiple items of same type
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.
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)
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*k
Here, W is the size of each element of the array, and k is the
index 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
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
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.
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 downward. Here, we design and implement
an algorithm to insert an element at the beginning of an array.
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.
Algorithm
1. Begin
2. IF N == MAX
return
3. ELSE N = N + 1
For all Elements in A Move to next adjacent location
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 downward. 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
SEEK Location index
For All Elements from A[index] to A[N]
Move to next adjacent location
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
Input index
For All Elements from A[index + 1] to A[N]
Move to next adjacent location
A[index + 1] = New Element
3. End
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