Professional Documents
Culture Documents
2.1 Array
An array is a series of elements of the same type placed in contiguous memory locations that
can be individually referenced by adding an index to a unique identifier. It is static, random
access data structure and elements are stored in physical contiguous locations.
Array can be 1-D, 2-D or n-D. Depending on language and platform, representation and
storage may vary. Let‟s see 1-D array and 2-D array in c language.
array 1 2 3 0 0
Here, for this array, size is n, it is number of value in between braces ({}). For above example
size is 5.
3. Searching: In Searching, its checking that weather the given element „e‟ is exist in
array or not. Searching techniques are discussed in unit-6.
4. Merging: Merging means combining two arrays of size n and m, in to one array of
size n + m.
For example a[3]={1,2,3}, b[5]={4,5,6,7,8} can be merge in array c, of size 3+4=7 as
follow.
C[7]={1,2,3,4,5,6,7,8}, red elements are from a, and black are from b.
5. Deletion: Process of removal of element from index i, from the array is called
deletion. Element cannot be deleted physically from array because array is static data
structure. But you can use rare value to indicate deletion, i.e. it can be just ignored.
For example use -999 to indicate deletion. If value is -999, ignore it. But here if value
-999 is data then also, it will be ignored. So use a value that never going to be stored.
All above operations can be performed on 2-D array.
#of column=4
# of row=2 1 2 3 4
5 6 7 8
In This example, elements will be stored in contiguous physical locations as follows, let‟s
assume that base address is 1048.
In column major order elements of array are stored column by column. All elements of first
column and then, all elements of second column, so on. For the same array, column major
order is shown below.
In 2-D array,
loc (a[i][j])=BA(a) +{[(i-lb1)*(no of column)+(j-lb2)]*c}
Here a is array stored in row major order, i is row, j is column ([i, j] index), loc(a[i][j]) is
address of index, [ i, j] and c is size of an element, lb1 is lower bound for i, lb2 is lower
bound for j.
No of column = ub2-lb2+1, ub2 is maximum value for j, lb2 is minimum index of j.
Example: Given a 2-D array a(2:6,6:11), stored in row major order with base address 2000,
size of each element is 2 byte. Find the address of a(4,8).
Here, i=4, j=9, And c=2, lb1=2, lb2=6, ub1=6, ub2=9 so
Loc(a[2][3]) = BA(a) +{[(i-lb1)*(no of column)+(j-lb2)]*c}
=2000 +{[(4-2)*(11-6+1)+(9-6)]*2}
=2000 +{[2*6+3]*2}
=2000 +{[12+3]*2}
=2000 +{15*2}
=2000+30 =2030
Simply,
Operations on 2-d array: all operations for 1-D array are also applicable for 2-d array. In
addition to it 2-d array multiplication can be performed.
Applications of array: There are many application of array, viz. it can be used to represent
list of items. Here we will study one essential application of it that is parse matrix
representation.
Sparse Matrix:
Generally 2-D, array used to represent matrix, so for n*m matrix we need at least enough
memory to store all entries that is m*n 2-d array. But what if there are many of entries are
zero, still we need m*n 2-D array.
Thus, if most of entries are zero in any matrix we call it sparse matrix. Simply in sparse
matrix at least m*n/2+1elements are zero. The basic idea to store sparse matrix is to store
only nonzero elements. So we can use different data structure to store sparse matrix. It yields
huge saving in memory requirements for matrix. One of these is array representation of
sparse matrix.
Upper right, lower left, upper left and lower right triangular matrixes are sparse matrixes.
Again unit matrix is sparse matrix.
0 1 2 0 0 0 1 2 0 0 0 0 1 0 0
0 0 3 1 0 0 3 0 0 0 0 1 0 1 0
0 0 0 2 3 0 0 0 0 0 2 3 0 0 1
Upper right lower left upper left lower right unit
So any sparse matrix can be represented by array.
0 0 0 List =
12 0 2 Row 2 2 3 3
0 0 0 Column 1 3 2 3
0 23 9 Value 12 2 23 9
Sparse Matrix
Stack: A stack is a particular kind of abstract data type or collection in which the principal
operations on the collection are the addition of an entity to the collection, known as push and
removal of an entity, known as pop. The relation between the push and pop operations is such
that the stack is a Last-In-First-Out (LIFO) data structure. In a LIFO data structure, the last
element added to the structure must be the first one to be removed. Stack has only one end
called top of the stack. We can add/remove (push/pop) elements only from stack top. Initially
top=-1. To add element increase top and insert at top. To remove acquire element and
decrease top.
Once your stack is full, you cannot add element to stack. Similarly when you stack is empty, there bo element
to remove so you cannot pop.
1.Push: You can insert element on the stack if there is room. First increase top and insert at
top. This operation is called push.
Algorithm push: This algorithm inserts an element (e) to stack(s) of size n, at top (t).
Input: s[],t, n, e
Output: Stack with one addition element and updated top.
if(t>n-1) then
write("Stack is full");
exit;
else
s[++t]=e;
end if
2.Pop: You can remove/delete element from stack top. Acquire element and then decrease
the top. this operation is called pop.
Algorithm pop: This algorithm deletes an element (e) from stack(s) of size n, from the
top (t).
Input: s[],t, n
Output: Stack with one less element and updated top and e.
if(t==-1) then
write(“stack is Empty”);
exit;
else
t=t-1;
return(s[t + 1]);
end if
Step 1: [initialization]
Index=0;
step 3:repeat step 3 until index<n-1
Algorithm peep: This algorithm return element from specified index from the top.
Input: s[],t, index
Output: e from given index
if(t-index)<0 then
write("underflow on peep");
return;
else
return(s[(t-index)]);
end if
Algorithm change: This algorithm update value at specified index from the top.
Input: s[],t, index, e
Output: update index at index
if(t-index<0) then
printf("underflow");
else
s[t-index]=e;
end if