Bartleby Related Questions Icon
Related questions
Question
Part A - Heapsort
We have included code in Java for a Binary Heap implementation.The first task is to use the heap to sort three separate arrays. The sorted arrays do not have to be "in place"
Use the implementation of the Binary Heap to:
(a) Sort a list of floats from high to low.
(b) Sort a list of strings alphabetically.
(c) Sort integers by their second digit only (low to high).
Here is the rest of code:
import java.util.ArrayList;
public class TestHeap {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(8);
list.add(5);
list.add(12);
list.add(12);
list.add(15);
list.add(20);
list.add(8);
list.add(5);
list.add(18);
list.add(5);
BinaryHeap<Integer> heap = new BinaryHeap<Integer>(new MaxIntegerComparorator());
heap.buildHeap(list);
heap.display();
}
}
import java.util.Comparator;
public class MaxIntegerComparorator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
}
import java.util.Comparator;
public class MinIntegerComparorator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2) * -1;
}
}
Transcribed Image Text:import java.util.ArrayList;
import java.util.Comparator;
public class BinaryHeap<T> {
protected ArrayList<T> heap;
protected Comparator<T> compareFunction = null;
public BinaryHeap (Comparator<T> compare Function) {
this.compareFunction = compareFunction;
}
protected int getLeft (int index) {
}
return 2* (index)+1;
protected int getRight (int index) {
return 2* (index)+2;
}
protected int getParent (int index) {
}
return index>0? (index+1)/2 -1 -1;
public void build Heap (ArrayList<T> list) {
heap = list;
for (int i=(heap.size())/2-1; i >= 0; i--) {
heapify(i);
}
}
public int size() {
}
return heap.size();
public T pop() {
if (heap.size() > 0) {
}
T top heap.get(0);
heap.set(0, heap.getLast());
heap.removeLast();
heapify(0);
return top;
else {
return null;
}
private void heapify(int index) {
int L= getLeft (index);
int R = getRight (index);
int optimal = index;
if (Lheap.size() && compareFunction.compare(heap.get(L),
heap.get(index)) > 0) {
}
optimal = L;
if (Rheap.size() && compareFunction.compare (heap.get(R),
heap.get(optimal)) > 0) {
optimal = R;
Transcribed Image Text:}
if (optimal != index) {
T tmp heap.get(index);
heap.set(index, heap.get(optimal));
heap.set(optimal, tmp);
heapify(optimal);
}
}
public void display() {
if (heap null) {
return;
}
int current Level = 0;
for (int i = 0; i < heap.size(); i++) {
int level = (int) (Math. log(i+1)/Math.log(2));
int maxLevels = (int) (Math.log(heap.size())/Math.log(2));
String spacing = "";
for (int j = 0; j < Math.pow(2, maxLevels-level); j++) {
spacing += "";
}
if (current Level <level) {
System.out.println();
current Level = level;
}
}
System.out.print(spacing + heap.get(i));
System.out.println();
Expert Solution
Check MarkThis question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
bartleby
Step by stepSolved in 2 steps
Knowledge Booster
Background pattern image
Similar questions
- JAVA 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_forwardC++Create 1 million integers randomly and store them in an array. Find the 100 smallest integers and print them. Use a Priority Queue implemented by Heap using array, and print the execution time too.arrow_forwardjava /* Practice Stacks and ourVector Write a java program that creates a stack of integers. Fill the stack with 30 random numbers between -300 and +300. A)- With the help of one ourVector Object and an additional stack, reorganize the numbers in the stack so that numbers smaller than -100 go to the bottom of the stack, numbers between -100 and +100 in the middle and numbers larger than +100 in the top (order does not matter) B)- (a little harder) Solve the same problem using only one ourVector object for help C)- (harder) Solve the same problem using only one additional stack as a helper */ public class HWStacks { public static void main(String[] args) { // TODO Auto-generated method stub } }arrow_forward
arrow_back_ios
arrow_forward_ios