Question
![Write the java programs to implement the following algorithms:
1. minHeapify (A, i)
2. buildMinHeap (A)
3. heapSort (A)
Use this code for help or guidance
package heaps;
public class Heap {
}
public static int[] maxHeapify(int[] A, int i) {
int left = 2*i;
int right = (2*i)+1;
int largest = 0;
if(left <=A. length && A[left]>A[i])
largest left;
else
largest = i;
if(right<=A. length && A[right] >A[largest])
largest right;
}
if(largest != i) {
int temp = A[largest];
A[largest] = A[i];
A[i] = temp;
maxHeapify (A, largest);
}
return A;
}
public static int [] buildMaxHeap (int [] A) {
for (int i (A. length-1)/2; i>=1; i--)
maxHeapify (A, i);
return A;
}
public static void main (String[] args) {
int[] arr = new int[10];
for (int i=1;i<arr.length; i++)
arr[i] (int) (Math.random()*100);
System.out.println("Before Heapifying");
for (int i=1
;i<arr. length; i++)
System.out.print(arr[i]+ ");
arr = buildMaxHeap (arr);
System.out.println();
System.out.println("After Heapifying");
for (int i-0; i<arr. length; i++)
System.out.print(arr[i]+ " ");](https://content.bartleby.com/qna-images/question/9b63d5f0-a313-4df7-92fd-2acb696a8a17/71c9f8b2-9f06-421a-8207-e2fbeadaaa7b/8zj7mqc_thumbnail.png)
Transcribed Image Text:Write the java programs to implement the following algorithms:
1. minHeapify (A, i)
2. buildMinHeap (A)
3. heapSort (A)
Use this code for help or guidance
package heaps;
public class Heap {
}
public static int[] maxHeapify(int[] A, int i) {
int left = 2*i;
int right = (2*i)+1;
int largest = 0;
if(left <=A. length && A[left]>A[i])
largest left;
else
largest = i;
if(right<=A. length && A[right] >A[largest])
largest right;
}
if(largest != i) {
int temp = A[largest];
A[largest] = A[i];
A[i] = temp;
maxHeapify (A, largest);
}
return A;
}
public static int [] buildMaxHeap (int [] A) {
for (int i (A. length-1)/2; i>=1; i--)
maxHeapify (A, i);
return A;
}
public static void main (String[] args) {
int[] arr = new int[10];
for (int i=1;i<arr.length; i++)
arr[i] (int) (Math.random()*100);
System.out.println("Before Heapifying");
for (int i=1
;i<arr. length; i++)
System.out.print(arr[i]+ ");
arr = buildMaxHeap (arr);
System.out.println();
System.out.println("After Heapifying");
for (int i-0; i<arr. length; i++)
System.out.print(arr[i]+ " ");
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 5 steps with 2 images

Knowledge Booster
Similar questions
- Need help with this Java problems. Java files are attached. Add your code to the file ArrayStack2Lab.java. Add your tests to the main() method. Submit ArrayStack2Lab.java. Problem 1 Implement the method public void display() which displays the entries in a stack starting from the top. If the stack is empty, print “The stack is empty”. Add the method to ArrrayStack2Lab.java. You do not need to modify StackInterface.java. Problem 2 Implement the method public int remove(int n) The method removes the n top most entries for a stack . If the stack contains less than n items, the stack becomes empty. The method returns the number of items removed. Add the method to ArrrayStack2Lab.java. You do not need to modify StackInterface.java.arrow_forwardAnswer in 5 mins else downvotearrow_forwardJAVA The following code for InsertionSort is given to us by the textbook. Trace the code stepby step using the array[55, 22, 77, 99, 66, 33, 11]on a piece of paper or using a Word document. If the code has errors, correct it and make itwork.public static void insertionSort(double[] list) {for (int i = 1; i < list.length; i++) {/** insert list[i] into a sorted sublist list[0..i-1] so thatlist[0..i] is sorted. */double currentElement = list[i];int k;for (k = i - 1; k >= 0 && list[k] > currentElement; k--) {list[k + 1] = list[k];}// Insert the current element into list[k+1]list[k + 1] = currentElement;}}arrow_forward
- Implement a c# program that builds a heap and then sorts it. ans with output screenshotarrow_forwardJavaarrow_forwardexplain this codes : class Heap{ int [] heapArray; int maxSize, heapSize; public Heap(int max) { maxSize=max; heapSize=0; heapArray = new int[maxSize]; } public boolean insert(int ele) { if((heapSize+1) == maxSize) return false; heapArray[++heapSize]=ele; int pos = heapSize; while(pos != 1 && ele < heapArray[pos/2]) { heapArray[pos] = heapArray[pos/2]; pos = pos/2; } heapArray[pos] = ele; return true; } public void displayHeap() { System.out.println("The contents of heap are: "); for(int i = 1;i <= heapSize;i++) System.out.print(heapArray[i]+" "); }}public class HeapTest { public static void main(String args[]){ Heap h=new Heap(7); h.insert(7); h.insert(8);h.insert(2);h.insert(4);h.insert(12);h.insert(5);h.displayHeap();}}arrow_forward
- Java codearrow_forwardThis is Java programming! The current code for BstMaxHeap is printing names backwords. So please modify the code BstMaxHeap.java so it prints as the following: The heap is not empty; it contains 9 entries. The largest entry is Whitney Removing entries in descending order: Removing Whitney Removing Regis Removing Megan Removing Matt Removing Jim Removing Jared Removing Doug Removing Brittany Removing Brettarrow_forward
arrow_back_ios
arrow_forward_ios