Analysis of Algorithm Chapter 1
Analysis of Algorithm Chapter 1
structures
Chapter 1
Part1
steps/execution
Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N x N
20
Read more on…..
21
Part2
Heap Sort
Cont’d…
• Heap sort operates by first converting the list
in to a heap tree.
• Heap tree is a binary tree in which each node
has a value greater than both its children (if
any). It uses a process called "adjust” to
accomplish its task (building a heap tree)
whenever a value is larger than its parent.
Cont’d…
Algorithm:
1. Construct a binary tree
The root node corresponds to Data[0].
If we consider the index associated with a particular
node to be i, then the left child of this node
corresponds to the element with index 2*i+1 and the
right child corresponds to the element with index
2*i+2. If any or both of these elements do not exist in
the array, then the corresponding child node does not
exist either.
Cont’d…