Question: Suppose we implement the HeapSort algorithm with the following code. HeapSort divides its input into a sorted and an unsorted region, and it iteratively shrinks

Suppose we implement the HeapSort algorithm with the following code. HeapSort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element from it and inserting it into the sorted region. Heap sort maintains the unsorted region in a heap data structure to more quickly find the largest element in each step. The HeapSort algorithm can be divided into two parts. In the first step, a max heap (root node is always great or equal to left/right nodes) is built out of the data (see line 27). The heap is often placed in an array with the layout of a complete binary tree. The complete binary tree maps the binary tree structure into the array indices; each array index represents a node; the index of the nodes parent, left child branch, or right child branch are simple expressions. For a zero-based array, the root node is stored at index 0; In the second step (starts from line 32), a sorted array is created by repeatedly removing the largest element from the heap (the root of the heap), and inserting it into the array. The heap is updated after each removal to maintain the heap property. Once all objects have been removed from the heap, the result is a sorted array. 1 public class HeapSort {2// Given a node at index i, arr[(i-1)/2] Returns the root/parent node. 3// arr[(2*i)+1] Returns the left child node. arr[(2*i)+2] Returns the right child node. 4 public static void heapify(int [] arr, final int i, final int len){5 int j = i; 6 while (true){7 int m = j; 8 if (m <= len/2){9 int c =2*m; 10 if (arr[c-1]< arr[m-1]) m=c; 11 if (c < len && arr[c]< arr[m-1]) m=c+1; 12}1314 if (m==j) break; 15 int tmp = arr[j-1]; 16 arr[j-1]= arr[m-1]; 17 arr[m-1]= tmp; 18 j = m; 19}20}212223 public static void sort(int [] arr){24// Array of size 1 is already sorted 25 if (arr.length <2) return; 2627// Step 1: Build heap 28 for (int i = arr.length/2; i >0; i--){29 heapify(arr,i,arr.length); 30}3132// Step 2: Now sort 33 int tmp; 34 for (int len = arr.length-1; len>1; len--){35 tmp = arr[0]; 36 arr[0]= arr[len]; 37 arr[len]= tmp; 38 heapify(arr,1,len); 39}40 tmp = arr[0]; 41 arr[0]= arr[1]; 42 arr[1]= tmp; 43}44} Please finish the following contracts for class HeapSort: (a) Pre-condition: the first input, i.e.,int [] arr, of heapify should not be null or empty. [2 points](b) Pre-condition: the inputs, i.e.,i and len should be less than or equal to the size of int [] arr.[2 points](c) Loop Invariant of while statement (line 6): during the process of heap building, each root is great than or equal to its left child node. [4 points](d) Loop Invariant of while statement (line 6): during the process of heap building, each root is great than or equal to its right child node. [4 points](e) Post-condition: after executing sort(int [] arr), the elements in arr are sorted in descending order. [4 points](f) Post-condition: the elements in arr are the same before and after executing sort(int [] arr).[4 points]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!