Implement HeapSort Algorithm 7.5, run it on 30 random numbers. In your submissio
ID: 3572461 • Letter: I
Question
Implement HeapSort Algorithm 7.5, run it on 30 random numbers. In your submission, show the initial heap after make heap and the heap after removing 5 elements. Include your source code in submission too. [Demo required during the week Sort n keys in no decreasing order. Inputs: positive integer n, array of n keys stored in an array implementation H of a heap. Outputs: the keys in no decreasing order in the array H.S. void heapsort (art n, heap & H) {make heap (n, H); remove keys (n, H, H, S);}Explanation / Answer
Non-decreasing means every next element isn't less then previous, and increasing means every next element is greater then previous.
Example : Increasing - 1 2 3 4
Non-decreasing - 1 1 2 3
The difference being that in an increasing sequence, for x(n) and x(n+1), x(n+1) > x(n) whereas in a non-decreasing sequence, x(n+1) >= x(n)
So by using Heapsort
BinaryHeap(T[] a, Comparator<T> c) {
this.c = c;
this.a = a;
n = a.length;
for (int i = n/2-1; i >= 0; i--) {
trickleDown(i);
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.