Could anyone please help me to understand this code:
That is java class which sorts array.
Questions:
1) What are the numbers of steps you needed to build the heap, and to destroy the heap?
2) How to get the sum of the construction and destruction steps?

public class HeapSorter { private static int[] a; private static int n; public static void sort(int[] a0) { a=a0; n=a.length; heapsort(); } private static void heapsort() { buildheap(); while (n>1) { n--; exchange (0, n); downheap (0); } } private static void buildheap() { for (int v=n/2-1; v>=0; v--) downheap (v); } private static void downheap(int v) { int w=2*v+1; // first descendant of v while (w<n) { if (w+1<n) // is there a second descendant? if (a[w+1]>a[w]) w++; // w is the descendant of v with maximum label if (a[v]>=a[w]) return; // v has heap property // otherwise exchange(v, w); // exchange labels of v and w v=w; // continue w=2*v+1; } } private static void exchange(int i, int j) { int t=a[i]; a[i]=a[j]; a[j]=t; }} // end class HeapSorter

Are you trying to actually count the instructions? In that case just make a global variable start it at 0 and increment it in the loop in downheap and nowhere else.
If you want the runtime, you don't

Since this is most likely a school assignment of some kind, we can't just give out answers but I can sure point you to how to solve it.

I don't see any destruction code, but the entire heap is stored in a single array. So how many steps would it take to 'destroy' that?

Your questions 1 and 2 appear to be the same. This is how you would count the construction steps. Look at how many times the for loop in buildheap runs. So for each of those you do downheap. What is the highest amount of steps that downheap could take? (Hint: the heap is a binary heap so the maximum height is known. You should know what it is too.)

Thanks for help i think i am getting there. How can i put those numbers into code. It would be ok with buildheap. How would i do with a downheap?
Theoretically it says h=log(Base2)(N+1)-1 - max hight.The thing is i want to generate that answer from code. Do you have any suggestions.

Are you trying to actually count the instructions? In that case just make a global variable start it at 0 and increment it in the loop in downheap and nowhere else.
If you want the runtime, you don't want or need it in the code. They want the average case runtime which you can see is O(n*lg(n))
The current hight of the heap can be calculated as the number of times the loop in downheap runs.

0

Featured Post

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.
https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt
https://filedb.experts-exchange.com/incoming/201…