Question

In: Computer Science

Can you please explain loop invariant for Heapsort, in which Min-Heapify and Build-Min-Heap are correct

Can you please explain loop invariant for Heapsort, in which Min-Heapify and Build-Min-Heap are correct

Solutions

Expert Solution

ANS: Loop Invariants- is a very simple powerful techniques. if algorithm or set of instruction is correct.the invariant is perform every node of heap property. the value of node is bigger than value of left and right children. there are following three important points-

Initialization- it is true prior to the first iteration of the loop.

Maintenance- it is true before an iteration of the loop ,it remains true before the next iteration.

Termination: when the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct. it is used mathmatical induction .

Algorithm-

Heap sort(A){

Build -max-heap(A)

for (i=A.length to down 2)

exchange(A[i],A[i])

A.heapsize = A. heap size-1

max-hepify(A,i)

}

Time complexity - O(nlogn).

min heapify - it is a complete binary tree.every node does not perform greater value of child nodes . every node does not contain a smalller value element than child node it is not min   heap.

Build min heap - is satisfies the heap property. the parent is less than or equal to child node .but max heap is opposite of min heap that parent is greater than to child node.

Algorithm-

Build -min-Heap(A)

heap-size [A] <----- length [A];

for i <---- length [A]/2 downnto 1

do min - Heapify (A,i);


Related Solutions

Let's suppose that you have a binary min-heap with S elements. Please write an an algorithm...
Let's suppose that you have a binary min-heap with S elements. Please write an an algorithm that will find all the elements that are in the heap that are smaller than a given value X. Please make sure that the time complexity of the algorithm that you propose has to be O(K), where is the number of elements smaller than X that is in the heap. Please type answer if you can
Suppose you have a min-heap in an array a[] where: ● The root isstored at...
Suppose you have a min-heap in an array a[] where: ● The root is stored at index 1 ● There are 15 elements (in a[1]...a[15]) ● There are no duplicates (this is not true in general for heaps; but assume it is true for this problem). Fill in the blanks in the statements below; the resulting statement must be true for any heap obeying the above properties.There are/is at least ____6_______ elements in theheap that are/is larger than a[3] ""There...
We can build a heap by repeatedly calling the insert function to insert the elements into...
We can build a heap by repeatedly calling the insert function to insert the elements into the heap. Here is pseudocode: buildHeap(A) h = new empty heap   for each element e in A       h.insert(e)             What is the Big-O runtime of this version of buildHeap? Justify your answer.
In this programming project, you will be implementing the data structure min-heap. You should use the...
In this programming project, you will be implementing the data structure min-heap. You should use the C++ programming language, not any other programming language. Also, your program should be based on the g++ compiler on general.asu.edu. All programs will be compiled and graded on general.asu.edu, a Linux based machine. If you program does not work on that machine, you will receive no credit for this assignment. You will need to submit it electronically at the blackboard, in one zip file,...
can you check if my answers are correct and can you please type the correct answers...
can you check if my answers are correct and can you please type the correct answers for each question 5 points) Trevor is interested in purchasing the local hardware/electronic goods store in a small town in South Ohio. After examining accounting records for the past several years, he found that the store has been grossing over $850 per day about 60% of the business days it is open. Estimate the probability that the store will gross over $850 at least...
Explain how you can utilize a minimum heap to sort the list of number in descending...
Explain how you can utilize a minimum heap to sort the list of number in descending order. Let n-be the number of elements in the list. What is the complexity of your sorting algorithm.Explain how you can utilize a minimum heap to sort the list of number in descending order. Let n-be the number of elements in the list. What is the complexity of your sorting algorithm.Please explain through example and give algorithem in written form (no code).
AS-AD model: Which one is the correct? and WHY? Could you please explain it to me...
AS-AD model: Which one is the correct? and WHY? Could you please explain it to me graphically (Which curves shift (AS/AD...) etc.) TIA When gov. reduces deficits by increasing taxes... in the short run, output decreases, and in the medium run, output Returns to potential in the short run, Government expenditures increase and thus in the medium run, output increases in the short run, interest rate is higher, and in the medium run, the effect of consolidation Policy on Investment...
Which of the following statements is NOT correct? (Please explain why answer E is correct) a)corporate...
Which of the following statements is NOT correct? (Please explain why answer E is correct) a)corporate governance is the set of rules that control a company's behavior towards its directors, managers, employees, shareholders, creditors, customers, competitors, and community b) agency problem is that managers may act in their own interests and not on behalf of stockholders. c) Corporate governance is the set of rules that control a company's behavior towards its directors, managers, employees, shareholders, creditors, customers, competitors, and community....
please explain methods that can be used for varying the speed of induction machine (min 4...
please explain methods that can be used for varying the speed of induction machine (min 4 method)
Please explain clearly which line means what and how can I get the answer. (correct answer...
Please explain clearly which line means what and how can I get the answer. (correct answer 2 2) public class Checker { public static int count = 0; public int number = 0; public Checker(){ count++; number = count; } public int getCount() { return count; } public int getNumber() { return number; } } What is output from the code fragment below? Checker one = new Checker(); Checker two = new Checker(); System.out.println(one.getCount() + " " + two.getCount());
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT