1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Virty [35]
3 years ago
12

Implement Heap (constructors, trickleUp and trickleDown), MyPriorityQueue (constructor, offer, poll, peek and isEmpty) and HeapS

ort (using heap directly or using PriorityQueue.poll). Comparators interface should be used. (20 points)
Engineering
1 answer:
muminat3 years ago
5 0

Answer:

public class HeapSort

{

public void sort(int arr[])

{

int n = arr.length;

 

for (int i = n / 2 - 1; i >= 0; i--)

heapify(arr, n, i);

 

for (int i=n-1; i>=0; i--)

{

int temp = arr[0];

arr[0] = arr[i];

arr[i] = temp;

 

heapify(arr, i, 0);

}

}

 

void heapify(int arr[], int n, int i)

{

int largest = i; // Initialize largest as root

int l = 2*i + 1; // left = 2*i + 1

int r = 2*i + 2; // right = 2*i + 2

 

if (l < n && arr[l] > arr[largest])

largest = l;

 

if (r < n && arr[r] > arr[largest])

largest = r;

 

if (largest != i)

{

int swap = arr[i];

arr[i] = arr[largest];

arr[largest] = swap;

 

heapify(arr, n, largest);

}

}

 

static void printArray(int arr[])

{

int n = arr.length;

for (int i=0; i<n; ++i)

System.out.print(arr[i]+" ");

System.out.println();

}

 

public static void main(String args[])

{

int arr[] = {12, 11, 13, 5, 6, 7};

int n = arr.length;

 

HeapSort ob = new HeapSort();

ob.sort(arr);

 

System.out.println("Sorted array is");

printArray(arr);

}

}

2. Priority Queue Program

import java.util.Comparator;

import java.util.PriorityQueue;

public class PriorityQueueTest {

static class PQsort implements Comparator<Integer> {

public int compare(Integer one, Integer two) {

return two - one;

}

}

public static void main(String[] args) {

int[] ia = { 1, 10, 5, 3, 4, 7, 6, 9, 8 };

PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>();

for (int x : ia) {

pq1.offer(x);

}

System.out.println("pq1: " + pq1);

PQsort pqs = new PQsort();

PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>(10, pqs);

for (int x : ia) {

pq2.offer(x);

}

System.out.println("pq2: " + pq2);

// print size

System.out.println("size: " + pq2.size());

// return highest priority element in the queue without removing it

System.out.println("peek: " + pq2.peek());

// print size

System.out.println("size: " + pq2.size());

// return highest priority element and removes it from the queue

System.out.println("poll: " + pq2.poll());

// print size

System.out.println("size: " + pq2.size());

System.out.print("pq2: " + pq2);

}

}

You might be interested in
From the article "Time Travel Is A Fun Science Fiction Story But Could It Be Real?", Albert Einstein's Theory of Relativity is d
zhannawk [14.2K]

Answer:

Option A

Explanation:

As per the article "Time Travel Is A Fun Science Fiction Story But Could It Be Real?" the gravitational field is a representation of curving space and time. As the gravity becomes strong, the space-time get more curved and hence the time gets slower.

Hence, option A is correct

8 0
3 years ago
The article provides information by using a list. What does it list? A. Thanksgiving food B. places where clams can be found C.
Gelneren [198K]

Answer:

C

Explanation:

7 0
3 years ago
In primary processing their are 3 different steps, what are the steps?
Nana76 [90]

Answer:

Primary processing involves cutting, cleaning, packaging, storage and refrigeration of raw foods to ensure that they are not spoilt before they reach the consumer.

8 0
3 years ago
Ayuda!<br> es para mañana
ExtremeBDS [4]
No se ve la foto :( puedes a ser la pregunta otra ves?
8 0
3 years ago
10POINTS
deff fn [24]

Answer:

Chinese

Explanation:

I hope this helps

3 0
3 years ago
Other questions:
  • Derive the probability that a receptor is occupied by a ligand using a model that treats the L ligands in solution as distinguis
    12·1 answer
  • A 15-ft beam weighing 570 lb is lowered by means of two cables unwinding from overhead cranes. As the beam approaches the ground
    9·1 answer
  • Methane (chemical formula Ch4), an important greenhouse gas, has a nearly constant mixing ratio throughout the lower atmosphere
    5·1 answer
  • What is the specific volume of oxygen at 40 psia and 80°F?
    10·1 answer
  • Light created high energy holes pass through the external circuits. (a)-True(T) (b)- false(F)
    13·1 answer
  • A wooden pallet carrying 540kg rests on a wooden floor. (a) a forklift driver decides to push it without lifting it.what force m
    10·1 answer
  • I think it’s a wedge. Am I right?
    8·2 answers
  • What do you do when two parts of a transfer line move at different rates?
    5·1 answer
  • Which of the following about valence electron is correct?
    10·2 answers
  • Okay<br>going offline bye<br>have a great day​
    10·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!