Question
Use the following heapsort code to find the time complexity (big O notation). heapSort is the primary method, and calls the other methods void heapSort(Element[]
Use the following heapsort code to find the time complexity (big O notation). heapSort is the primary method, and calls the other methods
void heapSort(Element[] E, int n){
int heapsize;
for(heapSize = n; heapSize >= 2; heapsize--){
Element curMax = E[1];
Element K = E[heapsize];
fixHeap(E, heapsize-1, 1, K);
E[heapsize] = curMax;
return;
}
void fixHeap(Element[] E, int heapSize, int root, Element K)
int left = 2*root, right = 2*root+1;
if(left > heapSize)
E[root] = K; // root is a leaf
else
// determine largerSubHeap
int largerSubHeap;
if(left == heapSize)
largerSubHeap = left; // no right SubHeap
else if(E[left].key > E[right].key)
largerSubHeap = left;
else
largerSubHeap = right;
// decide whether to filter K down
if(K.key >= E[largerSubHeap].key)
E[root] = K;
else
E[root] = E[largerSubHeap];
fixHeap(E, heapSize, largerSubHeap, K);
return;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started