Data Structures Notes
Data Structures Notes
Algorithm
An algorithm is a finite set of instructions that must be followed to solve a problem
OR
An algorithm is a logical representation of the instructions which should be executed to perform a
meaningful task.
Characteristics of an algorithm
1) input There are zero or more quantities supplied as input
2) output The result is available (atleast one quantity) is produced
3) Definiteness Each instruction should be unique, concise and clear.
4) Finiteness If the algorithm is traced out , then for all cases, the algorithm should terminate
after a finite number of steps
5) Effectiveness- Each instruction should be very basic and feasible.
Efficiency of an algorithm
After the designing of algorithm, the efficiency of algorithm must be analysed. Efficiency of algorithm
determines whether the algorithm is economical in use of computer resources i:e CPU time (running
time) and memory (memory space).
Complexity of Algorithm / Performance Analysis
The criteria on which we the complexity of algorithm is found out is
1) Does it do what it want to do it?
2) Does it work correctly accourding to the original specifications of the task?
3) Is there documentation that describes how to use it and how it works?
4) Are procedures created in such a way that they perform logical sub functions?
5) Is the code readable?
The other criteria on which algorithms are judged for the performance are computing time ( Time
complexity) and storage requirements (space complexity)
-
Time Complexity
The time complexity of an algorithm/program is the amount of computer time that it needs to
run to completion.
-
Space Complexity
The space complexity of an algorithm is the amount of memory need to run to completion.
5th element
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
Row major order - In row major order the elements of the first row occupies the first set of address ,
second row occupies the next set and so on.
For ex
The formula to calculate the address of any element in a matrix of size m x n ( m rows and n columns )
with each element of c bytes stored in row major order is
Loc ( A(i,j)) =L0 + [ ( i -1 ) * n + (j -1 ) ] * c
Ex : Location of A ( 3,2 ) in a matrix of 5 X 4 with base address of 5020 is
Location= 5020 + [(3-1) * 4 + (2-1 ) ] * 2 = 5038
1 2
3
4
5 6
7
8
The above array in linear fasion in row major order will be
9 10
11 12
13 14
15 16
1 18
2 3 194 20
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
17
First element at 5020
A(3,2) at location 5038
column major order - In column major order the elements of the first column occupies the first set of
address , second column occupies the next set and so on.
The formula to calculate the address of any element in a matrix of size m x n ( m rows and n columns )
with each element of c bytes stored in column major order is
Loc ( A(i,j)) =L0 + [ ( j -1 ) * m + (i -1 ) ] * c
Ex : Location of A ( 3,2 ) in a matrix of 5 X 4 with base address of 5020 is
Location= 5020 + [(2-1) * 5 + (3-1 ) ] * 2 = 5034
1
5
9
13
17
2
6
10
14
18
3
7
11
15
19
4
8
12
16
20
13 17 2
10 14 18 3
11 15 19 4
12 16 20
1)Bubble Sorting
It is a sorting technique in which two records are interchanged immediately when found that they
are not in proper order.
It requires at most n-1 (n-1 or less than n-1) passes to sort the elements.
During the first pass, a[1] and a[2] are compared, and if they are out of order, then a[1] and a[2]
are interchanged; this process is repeated for a[2] and a[3], a[3] and a[4] and so on.
This method will cause small values to move up or bubble up.
After the first pass, the record with the largest value will be in the nth position.
On each successive pass, the records with the next largest key will be placed in position n-1,n-2
and so on.. respectively and finally resulting in sorted array.
After each pass through the table, a check can be made to determine whether any swapping is
done during that pass or not. If no swapping occurred, then the table is sorted and no further
passes are required.
Insertion sort is the worst sort when the data is already in sorted order.
Complexity of Insertion sort
Complexity of insertion sort is O( N2 )
6) Shell Sort