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 int largest = 0; (2*1)+1; } if(left <=A. length && A[left]>A[i]) largest left; else largest = 1; 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

icon
Related questions
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]+ " ");
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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer