Using the provided MinHeap implementation & helper functions, build two sorted arrays as follows: Option 1: Given an unsorted array, build a sorted array in ascending order utilizing a minheap. Hint: this one is quite easy.. Load a minheap with your unsorted array. From there you can get the min element from the minheap & build your sorted array. Option 2: Given your unsorted array loaded into a maxheap.. Using this max-heap, in which the root node contains the largest element, build a sorted array in ascending order. To achieve this you’ll ‘Float’ the max to the front and build the sorted list from the back. The basic idea is to sort in place.     - Every time the algorithm polls from the heap, the heaps size shrinks by one.     - Hence, the next place at the end of the array is not part of the heap anymore.     - At this index the n-th smallest number is placed.    Final Solution should look like:   Array to be sorted by MinHeap: [5, 3, 17, 10, 84, 19, 6, 22, 9, 7, 28] Min Heap Sorted array: [3, 5, 6, 7, 9, 10, 17, 19, 22, 28, 84]   Array to be sorted by native Heapify: [5, 3, 17, 10, 84, 19, 6, 22, 9, 7, 28] Heapify sorted array: [3, 5, 6, 7, 9, 10, 17, 19, 22, 28, 84]   package keeper; class MinHeap { private int[] Heap; private int size; private int maxsize; // Initializing front private static final int FRONT = 1; // Ctor public MinHeap(int maxsize) { // This keyword refers to current object itself this.maxsize = maxsize; this.size = 0; Heap = new int[this.maxsize + 1]; Heap[0] = Integer.MIN_VALUE; } // Returning the position of // the parent for the node currently // at pos private int parent(int pos) { return pos / 2; } // Returning the position of the // left child for the node currently at pos private int leftChild(int pos) { return (2 * pos); } // Returning the position of // the right child for the node currently // at pos private int rightChild(int pos) { return (2 * pos) + 1; } // Returning true if the passed // node is a leaf node private boolean isLeaf(int pos) { if (pos > (size / 2.0)) { return true; } return false; } // swap two nodes of the heap private void swap(int fpos, int spos) { int tmp; tmp = Heap[fpos]; Heap[fpos] = Heap[spos]; Heap[spos] = tmp; } // heapify the node at pos. recursive call. private void minHeapify(int pos) { if (!isLeaf(pos)) { int swapPos = pos; // swap with the minimum of the two children to check if right child exists. // Otherwise default value will be '0' and that will be swapped with parent node. if (rightChild(pos) <= size) swapPos = Heap[leftChild(pos)] < Heap[rightChild(pos)] ? leftChild(pos) : rightChild(pos); else swapPos = Heap[leftChild(pos)]; if (Heap[pos] > Heap[leftChild(pos)] || Heap[pos] > Heap[rightChild(pos)]) { swap(pos, swapPos); minHeapify(swapPos); } } } //insert a node into the heap public void insert(int element) { if (size >= maxsize) { return; } Heap[++size] = element; int current = size; while (Heap[current] < Heap[parent(current)]) { swap(current, parent(current)); current = parent(current); } } // To print the contents of the heap public void print() { for (int i = 1; i <= size / 2; i++) { // Printing the parent and both children System.out.print( " PARENT : " + Heap[i] + " LEFT CHILD : " + Heap[2 * i] + " RIGHT CHILD :" + Heap[2 * i + 1]); // formating System.out.println(); } } // Remove and return the minimum // element from the heap public int remove() { int popped = Heap[FRONT]; Heap[FRONT] = Heap[size--]; minHeapify(FRONT); return popped; } // driver public static void main(String[] arg) { // Display message System.out.println("The Min Heap is "); // Creating object of class in main() method MinHeap minHeap = new MinHeap(29); // Inserting element to minHeap // using insert() method // Custom input entries minHeap.insert(5); minHeap.insert(3); minHeap.insert(17); minHeap.insert(10); minHeap.insert(84); minHeap.insert(19); minHeap.insert(6); minHeap.insert(22); minHeap.insert(9); minHeap.insert(7); minHeap.insert(28); // Print all elements of the heap minHeap.print(); // Removing minimum value from above heap // and printing it System.out.println("The Min val is " + minHeap.remove()); System.out.println("The Min val is " + minHeap.remove()); System.out.println("The Min val is " + minHeap.remove()); System.out.println("The Min val is " + minHeap.remove()); System.out.println("The Min val is " + minHeap.remove()); System.out.println("The Min val is " + minHeap.remove()); System.out.println("The Min val is " + minHeap.remove()); System.out.println("The Min val is " + minHeap.remove()); System.out.println("The Min val is " + minHeap.remove()); System.out.println("The Min val is " + minHeap.remove()); System.out.println("The Min val is " + minHeap.remove()); } }   Package keeper;  import java.util.Arrays; public class heap HW{ public static void main (String args[]) { int arr[] = {5, 3, 17, 10, 84, 19, 6, 22, 9, 7, 28 }; System.out.println("Array to be sorted by MinHeap: " + Arrays.toString(arr)); heapSortByMinHeap(arr); System.out.println(""); System.out.println("Array to be sorted by native Heapify: " + Arrays.toString(arr)); heapSortByHeapify(arr); }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Using the provided MinHeap implementation & helper functions, build two sorted arrays as follows:

Option 1:
Given an unsorted array, build a sorted array in ascending order utilizing a minheap.

Hint: this one is quite easy.. Load a minheap with your unsorted array. From there you can get the min element from the minheap & build your sorted array.

Option 2:
Given your unsorted array loaded into a maxheap..

Using this max-heap, in which the root node contains the largest element, build a sorted array in ascending order. To achieve this you’ll ‘Float’ the max to the front and build the sorted list from the back.

The basic idea is to sort in place.

    - Every time the algorithm polls from the heap, the heaps size shrinks by one.

    - Hence, the next place at the end of the array is not part of the heap anymore.

    - At this index the n-th smallest number is placed. 

 

Final Solution should look like:

 

Array to be sorted by MinHeap: [5, 3, 17, 10, 84, 19, 6, 22, 9, 7, 28]

Min Heap Sorted array: [3, 5, 6, 7, 9, 10, 17, 19, 22, 28, 84]

 

Array to be sorted by native Heapify: [5, 3, 17, 10, 84, 19, 6, 22, 9, 7, 28]

Heapify sorted array: [3, 5, 6, 7, 9, 10, 17, 19, 22, 28, 84]

 

package keeper;

class MinHeap {

private int[] Heap;

private int size;

private int maxsize;

// Initializing front

private static final int FRONT = 1;

// Ctor public MinHeap(int maxsize)

{

// This keyword refers to current object itself

this.maxsize = maxsize;

this.size = 0;

Heap = new int[this.maxsize + 1];

Heap[0] = Integer.MIN_VALUE; }

// Returning the position of

// the parent for the node currently

// at pos

private int parent(int pos) {

return pos / 2; }

// Returning the position of the

// left child for the node currently at pos

private int leftChild(int pos) {

return (2 * pos); }

// Returning the position of

// the right child for the node currently

// at pos

private int rightChild(int pos) {

return (2 * pos) + 1;

}

// Returning true if the passed

// node is a leaf node

private boolean isLeaf(int pos) {

if (pos > (size / 2.0)) {

return true;

}

return false;

}

// swap two nodes of the heap

private void swap(int fpos, int spos) {

int tmp;

tmp = Heap[fpos];

Heap[fpos] = Heap[spos];

Heap[spos] = tmp;

}

// heapify the node at pos. recursive call.

private void minHeapify(int pos) {

if (!isLeaf(pos)) {

int swapPos = pos;

// swap with the minimum of the two children to check if right child exists.

// Otherwise default value will be '0' and that will be swapped with parent node.

if (rightChild(pos) <= size)

swapPos = Heap[leftChild(pos)] < Heap[rightChild(pos)] ? leftChild(pos) : rightChild(pos);

else

swapPos = Heap[leftChild(pos)];

if (Heap[pos] > Heap[leftChild(pos)] || Heap[pos] > Heap[rightChild(pos)]) {

swap(pos, swapPos);

minHeapify(swapPos);

}

}

}

//insert a node into the heap public void insert(int element) {

if (size >= maxsize) { return;

} Heap[++size] = element;

int current = size;

while (Heap[current] < Heap[parent(current)]) {

swap(current, parent(current));

current = parent(current);

}

}

// To print the contents of the heap

public void print() {

for (int i = 1; i <= size / 2; i++) {

// Printing the parent and both children

System.out.print(

" PARENT : " + Heap[i] + " LEFT CHILD : " + Heap[2 * i] + " RIGHT CHILD :" + Heap[2 * i + 1]);

// formating

System.out.println();

}

}

// Remove and return the minimum

// element from the heap public int remove() {

int popped = Heap[FRONT];

Heap[FRONT] = Heap[size--];

minHeapify(FRONT);

return popped; }

// driver

public static void main(String[] arg) {

// Display message

System.out.println("The Min Heap is ");

// Creating object of class in main() method

MinHeap minHeap = new MinHeap(29);

// Inserting element to minHeap

// using insert() method

// Custom input entries

minHeap.insert(5);

minHeap.insert(3);

minHeap.insert(17);

minHeap.insert(10);

minHeap.insert(84);

minHeap.insert(19);

minHeap.insert(6);

minHeap.insert(22);

minHeap.insert(9);

minHeap.insert(7);

minHeap.insert(28);

// Print all elements of the heap

minHeap.print();

// Removing minimum value from above heap

// and printing it

System.out.println("The Min val is " + minHeap.remove());

System.out.println("The Min val is " + minHeap.remove());

System.out.println("The Min val is " + minHeap.remove());

System.out.println("The Min val is " + minHeap.remove());

System.out.println("The Min val is " + minHeap.remove());

System.out.println("The Min val is " + minHeap.remove());

System.out.println("The Min val is " + minHeap.remove());

System.out.println("The Min val is " + minHeap.remove());

System.out.println("The Min val is " + minHeap.remove());

System.out.println("The Min val is " + minHeap.remove());

System.out.println("The Min val is " + minHeap.remove());

}

}

 

Package keeper; 

import java.util.Arrays;

public class heap HW{

public static void main (String args[]) {

int arr[] = {5, 3, 17, 10, 84, 19, 6, 22, 9, 7, 28 };

System.out.println("Array to be sorted by MinHeap: " + Arrays.toString(arr));

heapSortByMinHeap(arr);

System.out.println("");

System.out.println("Array to be sorted by native Heapify: " + Arrays.toString(arr));

heapSortByHeapify(arr);

}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education