You are on page 1of 11

Arrays

Arrays are a linear data structure. Linear means its elements form a sequence, a list.
There is a linear relationship between the elements represented by means of sequential
memory locations. These linear structures are called arrays.
By linear array we mean a list of a finite number ‘n’ of similar data elements
referenced by a set of ‘n’ consecutive numbers. Usually 1, 2, 3,……, n.

Representation of an array for eg-


Int marks[10]
Int- data type
Marks- Name of the array
[10]- Total no of elements in the list

Individual elements are represented as marks[1], marks[2] etc


In general marks[k] here [k] called a subscript.

But theoretically sometime some other notations are also used to represent an array. For
eg- a1,a2,a3……………….an
Or by parenthesis notation
For eg A(1), A(2)……….,A(n)

Or bracket notations- A[1],A[2]………….,A[n]

The operations one normally performs with arrays are:-


1) Traversal- Processing each elements in the list
2) Search – finding the location of the element with a given value or record with a
given key
3) Insertion – Adding a new element to the list
4) Deletion – Removing an element from the list
5) Sorting – Arranging the elements in some type of order
6) Merging – Combining two lists into single list.
A linear array is a list of a finite number ‘n’ of homogenous data elements (i.e. data
elements of the same type) s.t.->

1. The elements of the array are referenced respectively by an index set consisting of
‘n’ consecutive numbers.
2. The elements of the array are stored respectively in successive memory locations.

The number n of the elements is called the length or size of the array.

If nothing is specified we assume the index set consists of integers 1,2,………,n.


In general the length or the number of data elements of the array can be obtained from
the index set by the formula.
Length = UB-LB+1
UB= Largest Index(called Upper Bound)
LB = Smallest Index(Lower Bound)
When LB=1
Then Length=UB

For Eg. Let marks be 7 element array

52 53 50 48 55 60 82 90 95

1 2 3 4 5 6 7 8 9

Marks[1] = 52
Marks[2] = 53……………………..Marks[7]=82

Representation of linear array in the memory


Let LA be a linear array in memory of the computer

LOC(LA(k))= address of the element LA(k) of the array LA


LOC Memory location

We know that all the elements of LA are stored in the successive memory locations/cells.
Thus computer doesn’t need record of address of all the elements of LA. But needs to
keep track of only the address of the first element of LA called Base(LA) i.e. base
address of LA . Using this address, the computer calculates the address of any element of
LA using the formula.

LOC(LA(k)) = Base(LA)+w(k-Lower bound)

Where
W= No of words / memory cell of array LA

e.g a[1]=20, a[2]=30, a[3]=40……………………..a[10]=100

let address of a[1] i.e. base(A)=200 find the location of element A[4] , when w=4.

LOC(A[4])= Base(A)+4(4-1)
= 200+4(3)
=200+12
=212-

Traversing Linear Arrays

Let A be the collection of data elements stored in memory. So we want to print all the
elements of an array or do some operation with each element of an array. This can be
accomplished by traversing (i.e. accessing and processing ) each element of A exactly
once.
Arrays –
1. Regular- where LB=1 and UB is some number n, where n>=1
2. Non- Regular- where LB!= 1 but some other +ve number and UB>=LB

Algorithm of Traversing
1. set K=LB
2. Repeat steps 3 and 4 while k<=UB
3. Apply Process to A[K]
4. set k=k+1
[end of step 2 loop]
5. Exit

OR
1. Repeat for K=LB to UB
Apply Process to LA[k]
[End of loop]
2. Exit

Inserting and Deleting

Inserting:- Adding another element to the collection .

Insertion at the end is easy. E.g

10 18 5 6

1 2 3 4 5 6 7 8 9 10
If we want to insert an element at the end of the array then we can simply set A[5]=5

To insert an element in the array the only condition is that enough memory space should
be there for addition. No new element can be inserted if there are no space available.
But suppose we need to insert an element in the middle of the array. Then on an average
half of elements will be moved downwards to accommodate new elements.

Similarly deleting an element from end causes no problem but deleting from somewhere
middle would require that each subsequent element be moved one location upwards in
order to ‘fill up’ the array.
Algorithm for Insertion:-
Let LA be an array with N element K location where element to be inserted. This
algorithm inserts element (ITEM) into the Kth position in LA

1. Set I=N
2. Repeat steps 3 and 4 while I>=k
3. Set LA[I+1]=LA[I]
4. Set I=I-1
[End of step 2 loop]
5 Set LA[K]=ITEM
6 Set N=N+1
7 Exit

Algorithm for Deletion:-

Let LA: linear Array with N elements


K : index location of the element to be deleted

1. Set ITEM= LA[k]


2. Repeat for J=K to N-1
Set LA[J]= LA[J+1]
[End of Loop]
3. Set N=N-1
4. Exit
Searching:
Finding an element in the array. Two type of search is

1. Linear Search
2. Binary Search

Linear Search Algorithm:-


 This is the most natural way of searching. In this method we simply go through a
list or a file till the required record is found or end of list or file is encountered.
Ordering of list is not important.
 The simplest technique to find out an element in an unsorted list.

Let A be an array with N elements and we have to search whether ITEM (element) is
present in the list, returns location of the element or set LOC=0 indicating that the
search is unsuccessful

1. Set A[N+1]=ITEM
2. Set LOC = 1
3. Repeat while A[LOC]!= ITEM
LOC=LOC+1
[End of loop]
4. If LOC=N+1, then set LOC=0
5. Exit

OR
1. Set T=0
2. Repeat for K=1 to N
3. If A[K]=ITEM then T=K
Return T
4. If T=0 then Unsuccessful Search
Otherwise Successful Search
Binary Search:-
In this case we will find out the middle element from the list & compare it with the
element to be searched. If both are same our searching completes otherwise if element
to be searched is less than middle element then we will make search towards the left
of the middle otherwise we will search toward the right of the list and this process
will continue until element is not found or there is no more element to be compared
with element to be searched.
 It is an efficient algorithm. It takes less time.
 It requires the array elements to be arranged in sorted order either ascending or
descending order.
Consider a list of elements
2 3 8 10 12 15 18
Suppose we have to search whether 15 is present in the list or not.
Here LB = 1
UB = 7
Item=15
Find Mid point =(1+7)/2= 8/2=4
Compare Item with 4th element of array. 15>10
Therefore LB = 4+1=5(midpoint + 1)
UB = 7
Midpoint (5+7)/2= 12/2=6
Compare Item with 6th element of the array.
And 15=15
Search successful.
Binary Search Algorithm
Let DATA be a sorted array
1. set BEG = LB, END = UB and MID = (BEG+END)/2
2. Repeat steps 3 and 4 while BEG <= END and
DATA[MID] != ITEM
3. If ITEM < DATA[MID] then
Set END = MID-1
Else
Set BEG = MID+1
4. Set MID = INT((BEG+END)/2)
5. If DATA[MID] = ITEM
Set LOC = MID
Else
Set LOC = NULL
6. Exit

Sorting:-

 Sorting refers to the operation of arranging data in some given order, such as
increasing or decreasing, with numerical data or alphabetically, with character
data.
BUBBLE SORT

 In bubble sort, the file is passed through sequentially several times. In each pass
each element is compared with its successor. i.e (x[i] with x [i+1] and
interchanged if they are not in the proper order.
 There are at most n-1 passes required.

32 51 27 85 66 23 13 57
32 27 51 85 66 23 13 57
32 27 51 66 85 23 13 57
32 27 51 66 23 85 13 57
32 27 51 66 23 13 85 57
32 27 51 66 23 13 57 85
Repeat the same process with this list only
This is the phase 1 after that largest element is at its own position
Algorithm:-
1. Repeat steps 2 and 3 for k=1 to N-1
2. Set ptr=1
3. Repeat while ptr<=N-k
a) if DATA[ptr] > DATA[ptr+1] then
Interchange DATA[ptr] and DATA[ptr+1]
b) Set ptr = ptr+1
4. Exit

If there are n elements then the time required to sort elements is n(n+1)/2

Multi Dimensional Arrays


In one dimensional array each element in the array is referenced by a single subscript. In
multi dimensional arrays elements are referred by more than one subscript.
2 Dimensional Arrays:-
A 2D m*n array A is a collection of m*n number of elements. Each element is specified
by two subscripts J and K with the property that
1<=J<=m and 1<=k<=n
The element of A will be denoted by A[J,K]. A 2D arrays are called matrices where
m is the number of rows
n is the number of columns
Representation of 2D array in memory-
Let A be a 2D m*n array. It requires m*n sequential memory location for its
storage. Elements may be stored in row major or the column major order.
Column major order Row major order
To compute the address of a particular element in the array the formula is
Column major order-
Loc(A[J,K]) = Base(A) + w[M(K-1) + (J-1)]
Row major order-
Loc(A[J,K]) = Base(A) + w[N(J-1) + (K-1)]
Here
Base(A)- base address of array
w- no of bytes for each element
M- total no of rows
N- total no of columns

MATMUL(A,B,C,M,P,N)
Let A be an M*P matrix array, and let B be a P*N matrix array. This algo stores
the product of A and B in an M*N array C.
1. Repeat steps 2 to 4 for I = 1 to M
2. Repeat steps 3 and 4 for j=1 to N
3. set C[I,J]=0
4. Repeat for K=1 to P
C[I,J]=C[I,J]+A[I,K]*B[K,J]
[End of inner Loop]
[End of step 2 middle loop]
[End of step 1 outer loop]
5. Exit

You might also like