Data Structures (R16) Unit-I
Data Structures (R16) Unit-I
Data Structures (R16) Unit-I
Properties
Every algorithm must satisfy the following properties:
When we want to analyze an algorithm, we consider only the space and time required
by that particular algorithm and we ignore all remaining elements.
Performance analysis of an algorithm is performed by using the following
measures:
1. Space Complexity
2. Time Complexity
NOTE: When we want to perform analysis of an algorithm based on its Space complexity,
we consider only Data Space and ignore Instruction Space as well as Environmental Stack.
That means we calculate only the memory required to store Variables, Constants,
Structures, etc.,
Consider the following piece of code...
int square(int a)
{
return a*a;
}
In above piece of code, it requires 2 bytes of memory to store variable 'a' and another 2
bytes of memory is used for return value. That means, totally it requires 4 bytes of
memory to complete its execution.
Example
Consider the following piece of code...
Algorithm Search (A, n, x)
{ // where A is an array, n is the size of an array and x is the item to be searched.
for i := 1 to n do
{
if(x=A[i]) then
{
write (item found at location i)
return;
}
}
write (item not found)
}
The time complexity for the above algorithm
1. Best case is Ω (1)
2. Average case is Θ (n/2)
3. Worst case is O (n)
Based on the organizing method of a data structure, data structures are divided into two
types.
1. Linear Data Structures
2. Non - Linear Data Structures
Advantages:
Code is easier to understand.
Implementations of ADTs can be changed without requiring changes to the
program that uses the ADTs.
ADTs can be reused in future programs.
ADT Model
The ADT model is shown in below figure. It consists of two different parts:
1. Public part
2. Private part
Computer does not need to keep track of the address of every element of LA, but need to
track only the address of the first element of the array denoted by
Base(LA)
and called the base address of LA. Using this address, the computer calculates the
address of any element of LA by the following formula:
LOC(LA[K]) = Base(LA) + w(K – LB)
where w is the number of words per memory cell of the array LA [w is the size of the
data type] .
Example: Find the address for LA [6]. Each element of the array occupy 1 byte
LOC(LA[K]) = Base(LA) + w(K – lower
bound)
LOC(LA[6]) = 200 + 1(6 – 0) = 206
1.6.1 Traversing
Let A be a collection of data elements stored in the memory of the computer.
Suppose we want to print the content of each element of A or count the number of
elements of A with a given property. This can be accomplished by traversing A, that is,
by accessing and processing (frequently called visiting) each element of A exactly once.
Output
The array elements are 12 45 -10 3 28
1.6.2 Insertion
Let A be a collection of data elements in the memory of the computer. “Inserting”
refers to the operation of adding another element to the collection A,
Inserting an element at the ‘end’ of a linear array can be easily done. On the other
hand, suppose we need to insert an element in the middle of the array. Then on the
average, half of the elements must be moved downward to new locations to
accommodate the new element and keep an order of the order of the other elements.
void main()
{ Output
int a[100], n,element,i, pos; Enter size of an array : 5
clrscr(); Enter elements: 1 2 3 4 5
printf("\nEnter size of an array:"); Enter the element to be inserted : 6
scanf("%d", &n); Enter the location : 2
printf("\nEnter elements :"); Resultant Array: 1 6 2 3 4 5
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("\nEnter the element to be inserted :");
scanf("%d", &element);
n++;
a[pos - 1] = element;
1.6.4 Sorting
Sorting means arranging the elements of an array in specific order may be either
ascending or descending. There are different types of sorting techniques are available:
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that
repeatedly steps through the list to be sorted, compares each pair of adjacent items and
swaps them if they are in the wrong order.
Let A be a list of ‘n’ numbers. Sorting A refers to the operation of re-arranging the
elements of A, so they are in increasing order, i.e. so that
For example, Suppose A originally is the list 8, 4, 19, 2, 7, 13, 5, 16. After sorting, A is the
list 2, 4, 5, 7, 8, 13, 16, 19.
1.6.5 Searching
Searching means to find whether a particular value is present in an array or not.
If the value is present in the array, then searching is said to be successful and the
searching process gives the location of that value in the array. If the value is not present
in the array, the searching is said to be unsuccessful. There are two popular methods for
searching the array elements:
1. Linear search
2. Binary search.
Algorithm
LINEAR SEARCH (A, N, ITEM, LOC).
Here, A is a linear array with N elements, and ITEM is a given item of information. This
algorithm finds the location LOC of ITEM in A, or sets LOC := -1 if the search is
unsuccessful.
Step 1: [Initialize Location.] Set LOC := -1.
Step 2: [Initialize Counter.] Set I:= 0.
Step 3: Repeat steps 3 and 4 while I<N [Search for ITEM.]
IF A[I] = ITEM
Set := LOC := I.
Write(Search successful or ITEM found)
P(x) = 4x3+6x2+7x+9
A polynomial thus may be represented using arrays. A single dimensional array
is used for representing a single variable polynomial. The index of such array can be
considered as an exponent and coefficient can be stored at that particular index. The
array representation for the above polynomial expression is given below:
Advantages:
1. Easy to handle
Drawbacks
1. It is time consuming
2. Wastage of memory space
3. We can change the size of an array
Output
Polynomial one is : -7(x)5 + 4(x)2 +1(x)1
Polynomial two is : -3(x)4 + 5(x)3 + 6(x)2 + 10(x)1 – 14
Resultant polynomial is : -7(x)5 + 3(x)4 + 5(x)3 + 10(x)2 + 11(x)1 – 14
The two-dimensional arrays are also called matrices in mathematics and tables business
applications. Hence 2d arrays are sometimes called matrix arrays.
The standard way of representing 2-d arrays:
Storage Representations
Let A be a two-dimensional m x n array. The array A will be represented in the
memory by a block of m x n sequential memory locations. Programming language will
store array A either
1. Column-Major Order
2. Row-Major Order
If a two-dimensional array can be represented as a single row with many columns and
mapped sequentially is known as column-major representation.
Algorithm: MATMUL(AB,C,M,P,N)
Let A be an MxP matrix array , and Let B be an PxN matrix array. This algorithm stores
the product of A and B in MxN matrix array C.
printf("\n");
}
}
else
printf("Multiplication is not possible\n");
getch();
}
Output
Enter number of rows and columns of first matrix (between 1 and 10):2 3
Enter number of rows and columns of second matrix (between 1 and 10):3 2
Enter elements of first matrix: 1 2 3 4 5 6
Enter elements of second matrix: 1 2 3 4 5 6
The Matrix A
1 2 3
4 5 6
The Matrix B
1 2
3 4
5 6
The resultant matrix is
22 28
49 64
In triangular matrix, all entries above the main diagonal are zero or equivalently, where
nonzero entries can only occur on or below the main diagonal.
Triplet Representation
In this representation, we consider only non-zero values along with their row
and column index values. In this representation, the 0th row stores total rows, total
columns and total non-zero values in the matrix.
For example, consider a matrix of size 5 X 6 containing 6 number of non-zero
values. This matrix can be represented as shown in the table:
In above example matrix, there are only 6 non-zero elements ( those are 9, 8, 4, 2,
5 & 2) and matrix size is 5 X 6. We represent this matrix as shown in the above table.
Here the first row in the right side table is filled with values 5, 6 & 6 which indicate that
it is a sparse matrix with 5 rows, 6 columns and 6 non-zero values. Second row is filled
with 0, 4, and 9 which indicates the value in the matrix at 0th row, 4th column is 9. In
the same way the remaining non-zero values also follows the similar pattern.
Linked Representation
In linked representation, we use linked list data structure to represent a sparse
matrix. In this linked list, we use two different nodes namely header node and element
node. Header node consists of three fields and element node consists of five fields as
shown in the fig.
In above representation, H0, H1,...,H5 indicates the header nodes which are used to
represent indexes. Remaining nodes are used to represent non-zero elements in the
matrix, except the very first node which is used to represent abstract information of the
sparse matrix (i.e., It is a matrix of 5 X 6 with 6 non-zero elements).
In this representation, in each row and column, the last node right field points to it's
respective header node.
Output
Enter no. of rows, cols and non-zero elements:6 6 8
Enter the next triplet(row, column, value): 0 0 15
Enter the next triplet(row, column, value): 0 3 22
Enter the next triplet(row, column, value): 0 5 -15
Enter the next triplet(row, column, value): 1 1 11
Enter the next triplet(row, column, value): 1 2 3
Enter the next triplet(row, column, value): 2 3 -6
Enter the next triplet(row, column, value): 4 0 91
Enter the next triplet(row, column, value): 5 2 28
******Sparse matrix*********
row column value
6 6 8
0 0 15
0 3 22
0 5 -15
1 1 11
1 2 3
2 3 -6
4 0 91
5 2 28
******After Transpose*********
row column value
6 6 8
0 0 15
3 0 22
5 0 -15
1 1 11
2 1 3
3 2 -6
0 4 91
2 5 28