Unit 1 Sequential Organization
Unit 1 Sequential Organization
ORGANIZATION
CONCEPT OF SEQUENTIAL
ORGANIZATION:
• It mean data is stored in sequential form, in consecutive
memory locations.
• Ex. Array
• There are two basic operations performed on this data.
i. Storing data at desired location.
ii. Retrieving data from desired location.
LINEAR DATA STRUCTURE
• Manipulated by position
• Linear list
• Eg- days of week, digits etc.
• integer = {0, +1, -1, +2, -2, +3, -3, …}
• daysOfWeek = {S,M,T,W,Th,F,Sa}
• Data structures used: Array(static & dynamic) & linked list
• Operations: Display, Search, Insert, Delete
ORDERED LISTS
0 10
Value
1 20 stored in
Index array
used to 2 30
find 3 40
element
.
.
.
9
ARRAYS
Solution
❑ In the first case, we need 100 instructions to read, 100
instructions to write and 100 instructions to process. The
total is 300 instructions.
❑ In the second case, we have three loops. In each loop we have
two instructions, for a total of six instructions. However, we
also need three instructions for initializing the index and three
instructions to check the value of the index. In total, we have
twelve instructions.
Example
The number of cycles (fetch, decode, and execute phases) the
computer needs to perform is not reduced if we use an array. The
number of cycles is actually increased, because we have the extra
overhead of initializing, incrementing and testing the value of the
index. But our concern is not the number of cycles: it is the
number of lines we need to write the program.
Array name versus element name
In an array we have two types of identifiers: the name of the
array and the name of each individual element. The name of
the array is the name of the whole structure, while the name
of an element allows us to refer to that element.
0 1 2
0 10 20 30
1 40 50 60
.
.
9
COLUMN MAJOR REPRESENTATION
• If the elements are stored in column wise manner then it is
called “Column Major Representation”.
• Ex. If we want to store elements
• 10 20 30 40 50 60 then elements will be filled up by column
wise manner (consider array a[3][2])
• In a 2D array
0 1
0
10 40
1
20 50
2
30 60
• Each element is occupied at successive location if the
element is of integer type then 2 bytes of memory
will be allocated.
• If it is float then 4 bytes of memory will be allocated.
• Ex.
• int a[3][2]={ {10,20}
{30,40}
{50,60} }
• Then in a row major matrix
a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] a [2][1]
10 20 30 40 50 60
10 30 50 20 40 60
• By using structure.
• 2 main advantages
• 1. There is no limit on maximum value of the exponent.
• 2. It requires less number of terms though there is vast
difference in max & min value of exponent.
• Ex.
typedef struct ploy
{
int coeff;
int expo;
}p;
p p1[10];
Ex. 7x999-10
Coeff expo
0
7 999
1 -10 0
.
.
.
9
POLYNOMIAL ADDITION
• Ex.
• If two polynomial are given as
3x3+2x2+x+1
x 5x3+7x
Algorithm:
Step1: Read the polynomial array A.
Step2: Read the value of x.
Step3: Initialize the variable sum to zero.
Step 4: Then calculate coeff * pow (x, exp) of each term and
add the result to sum.
Step 5: Display sum.
Step 6 : stop
POLYNOMIAL – ABSTRACT DATA TYPE (ADT)
Object: Polynomial.
Operations:
◦ Boolean IsZero(poly)
::= return FALSE or TRUE.
• Definition
• Sparse matrix is that matrix which has a very few non zero
elements.
• Representation
• Ex.
• Suppose a matrix is 6X7 & number of non-zero elements are say 8
then representation will be
Index Row No Column value
No
0 6 7 8
1 0 6 -10
2 1 0 55
3 2 5 -23
4 3 1 67
5 3 6 88
6 4 3 14
7 4 4 -28
8 5 0 99
Sparse Matrices
row1
row2
row3
row4
row5
5*3 6*6
15/15 8/36
sparse matrix
data structure?
SPARSE MATRIX REPRESENTATION
• In normal Matrix –
• Space will be 6*6*2=72 bytes
• In sparse matrix –
• (TOTAL NO. OF NON-ZERO VALUE +1)*3*2
(8+1)*3*2=9*6 =54 bytes
Sparse matrix representation saves (72-54=18 bytes) of
memory.
.
REPRESENTATION FOR SPARSE
MATRICES
typedef struct {
int col, row, value;
} term;
term a[MAX_TERMS];
READ SPARSE
• Conventions
• Order of 2 matrices are same.
• If there is an element at <i ,j> in one matrix & also an element at the
position in another matrix then add two elements & store the sum in
resultant matrix.
• Otherwise copy both the elements in resulting matrix.
ALGORITHM:
• 1. Start
• 2.Read two sparse matrices SP1 and SP2
• 3.The index of SP1 and SP2 will be i=1,j=1 resp. Non zero
elements are t1 and t2 for SP1 and SP2.
• 4.The k=1 index will be for sparse matrix SP3 which will store
the addition of two matrices.
• 5. SP3[0][0]=SP1[0][0]
• SP3[0][1]=SP1[0][1]
Row Col Val Row Col Val
3 3 4 3 3 4
0 0 1 0 0 1
1 0 2 0 1 2
2 0 3 1 0 3
2 1 4 2 1 4
SP1 SP2
Row Col Val
3 3 5
0 0 2
0 1 2
1 0 5
2 0 3
2 1 8
SP3
OPERATIONS: TRANSPOSE
• c transpose(a) // a: m x n matrix
//Algorithm 1:
for each row i {
take element (i, j, value) and
store it as (j, i, value).
}
row col value row col value
• Eg.
a[0] 6 6 8 c[0] 6 6 8
a[1] 0 0 15 c[1 ] 0 0 15
a[2] 0 3 22 c[2] 3 0 22
a[3] 0 5 -15 c[3] 5 0 -15
a[4] 1 1 11 c[4] 1 1 11
a[5] 1 2 3 c[5] 2 1 3
a[6] 2 3 -6 c[6] 3 2 -6
a[7] 4 0 91 c[7] 0 4 91
a[8] 5 2 28 c[8] 2 5 28
Index Row No Column value
No
0 6 7 8
Find Transpose 1 0 6 -10
of this sparse
matrix. 2 1 0 55
3 2 5 -23
4 3 1 67
5 3 6 88
6 4 3 14
7 4 4 -28
8 5 0 99
Index Row No Column value
No
0 7 6 8
1 0 1 55
2 0 5 99
3 1 3 67
4 3 4 14
5 4 4 -28
6 5 2 -23
7 6 0 -10
8 6 3 88
OPERATIONS: TRANSPOSE
Problem: If we just place them consecutively, we need to
do a lot of insertions to make the ordering right.
c[0] 6 6 8
c[1 ] 0 0 15
c[2] 3 0 22
c[3] 5 0 -15
c[4] 1 1 11
c[5] 2 1 3
c[6] 3 2 -6
c[7] 0 4 91
c[8] 2 5 28
ALG. 2 FOR TRANSPOSE
• Algorithm 2:
• Find all elements in col. 0, and store them in row 0;
Find all elements in col. 1, and store them in row 1; … … … …
etc
a[0] 6 6 8 c[0] 6 6 8
a[1 ] 0 0 15 c[1 ] 0 0 15
a[2] 0 3 22 c[2] 0 4 91
a[3] 0 5 -15 c[3] 1 1 11
a[4] 1 1 11 c[4] 2 1 3
a[5] 1 2 3 c[5] 2 5 28
a[6] 2 3 -6 c[6] 3 0 22
a[7] 4 0 91 c[7] 3 2 -6
a[8] 5 2 28 c[8] 5 0 -15
ALG. 2 FOR TRANSPOSE
Algorithm 2 :
for (j=0; j<#col; j++) { //O(#col)
Running time = O(#col x #terms)
for all elements in col j { //O(#terms)
place element (i, j, value) in the
next position of array c[];
}
}
SIMPLE TRANSPOSE
B[0][0]=A[0][1];
B[0][1]=A[0][0];
B[0][2]=A[0][2];
noterms=A[0][2];
noc=A[0][1];
if(A[0][2]>1)
{
nxt=1;
for(c=0;c<noc;c++)//loop till col number of nonzero elements
{ for(Term=1;Term<=noterms;Term++)// loop till we have nonzero
elements
{
/* if a column number of current triple == c, then insert the current triple in B */
if(A[Term][1]== c)
{
B[nxt][0]=A[Term][1];
B[nxt][1]=A[Term][0];
B[nxt][2]=A[Term][2];
nxt++;
}
}
}
}
COMPLEXITY OF SIMPLE TRANSPOSE
}
}
}
CHAPTER 2 60