Professional Documents
Culture Documents
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.
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)
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.
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
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.
Where
W= No of words / memory cell of array LA
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-
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
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
1. Linear Search
2. Binary Search
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
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