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
If you get a flat in the front of your car, your car will:
juin [17]

Answer:

stop and might even crash

Explanation:

6 0
3 years ago
Need help giving out brainlest
Kazeer [188]

Answer:

1,2,3,5

Explanation:

Not positive but i think so

6 0
3 years ago
As of January 1, 2018, Farley Co. had a credit balance of $534,000 in its allowance for uncollectible accounts. Based on experie
zepelin [54]

Answer:

Mah dood! I gatchu! Dae ansa iz A! I gatchu fam! I gatchu Brochacho!

Explanation:

3 0
3 years ago
Harmony in music is characterized by _____.
stira [4]

Answer:

the vertical relationship of pitches.

Explanation:

Harmony can be defined as the use of simultaneous pitches ( two or more tones and notes) or chords played at the same time together.

Harmony in music is characterized by the vertical relationship of pitches. The three most popular and essential forms of harmony are;

1. Diatonic harmony.

2. Non-diatonic harmony.

3. Atonal harmony.

6 0
3 years ago
Read 2 more answers
Please Help It's a lot I'm sorry. =(
kow [346]

it the first one hope this helps

8 0
3 years ago
Read 2 more answers
Other questions:
  • Suppose that the president of a small island nation has decided to increase government spending by constructing three beach reso
    11·1 answer
  • Derive an expression for the axial thrust exerted by a propeller if the thrust depends only on forward speed, angular speed, siz
    6·1 answer
  • A soil sample 90 mm high and 6000 mm2 in cross section was subjected to a falling head permeability test. The head fell from 500
    7·1 answer
  • Assume the average fuel flow rate at the peak torque speed (1500 rpm) is 15kg/hr for a sixcylinder four-stroke diesel engine und
    11·1 answer
  • 5. Identify the pros and cons of<br> manufactured siding.
    12·1 answer
  • Suppose a student rubs a Teflon rod with wool and then briefly touches it to an initially neutral aluminum rod suspended by insu
    6·1 answer
  • Given the strings s1 and s2, not necessarily of the same length, create a new string consisting of alternating characters of s1
    5·1 answer
  • THE COMPUND INTEREST ON RS 30,000AT 7% PER ANNUM IS RS 4347 THE PERIOD IN YEARS
    7·2 answers
  • Where can I find solved problems of advanced soil structure interaction?​
    6·2 answers
  • What do we need to do to get CO2 emissions all the way to zero?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!