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
Which of the following can not be used to store an electrical charge?
Nataly_w [17]
I’ve been feeeling very lonely
3 0
3 years ago
What is the function and role of product tear down charts, and how do engineers utilize them in the reverse engineering process?
lora16 [44]

Answer:

Product Teardown 28 pieces (1) Plastic packaging: protect and display product for purchase. (4) Exterior screws: hold case halves together. (1) Right case half: acts as part of a handle and contains the rest of the parts. (1) Left case half: acts as part of a handle and contains the rest of the parts.

Explanation:

A product teardown process is an orderly way to know about a particular product and identify its parts, system functionality to recognize modeling improvement and identify cost reduction opportunities. Unlike the traditional costing method, tear down analysis collects information to determine product quality and price desired by the consumers.

7 0
3 years ago
A thick aluminum block initially at 26.5°C is subjected to constant heat flux of 4000 W/m2 by an electric resistance heater whos
Yanka [14]

Given Information:

Initial temperature of aluminum block = 26.5°C

Heat flux = 4000 w/m²

Time = 2112 seconds

Time = 30 minutes = 30*60 = 1800 seconds

Required Information:

Rise in surface temperature = ?

Answer:

Rise in surface temperature = 8.6 °C after 2112 seconds

Rise in surface temperature = 8 °C after 30 minutes

Explanation:

The surface temperature of the aluminum block is given by

T_{surface} = T_{initial} + \frac{q}{k} \sqrt{\frac{4\alpha t}{\pi} }

Where q is the heat flux supplied to aluminum block, k is the conductivity of pure aluminum and α is the diffusivity of pure aluminum.

After t = 2112 sec:

T_{surface} = 26.5 + \frac{4000}{237} \sqrt{\frac{4(9.71\times 10^{-5}) (2112)}{\pi} }\\\\T_{surface} = 26.5 + \frac{4000}{237} (0.51098)\\\\T_{surface} = 26.5 + 8.6\\\\T_{surface} = 35.1\\\\

The rise in the surface temperature is

Rise = 35.1 - 26.5 = 8.6 °C

Therefore, the surface temperature of the block will rise by 8.6 °C after 2112 seconds.

After t = 30 mins:

T_{surface} = 26.5 + \frac{4000}{237} \sqrt{\frac{4(9.71\times 10^{-5}) (1800)}{\pi} }\\\\T_{surface} = 26.5 + \frac{4000}{237} (0.4717)\\\\T_{surface} = 26.5 + 7.96\\\\T_{surface} = 34.5\\\\

The rise in the surface temperature is

Rise = 34.5 - 26.5 = 8 °C

Therefore, the surface temperature of the block will rise by 8 °C after 30 minutes.

5 0
3 years ago
a cantilever beam 1.5m long has a square box cross section with the outer width and height being 100mm and a wall thickness of 8
djverab [1.8K]

Answer:

a) 159.07 MPa

b) 10.45 MPa

c) 79.535 MPa

Explanation:

Given data :

length of cantilever beam = 1.5m

outer width and height = 100 mm

wall thickness = 8mm

uniform load carried by beam  along entire length= 6.5 kN/m

concentrated force at free end = 4kN

first we  determine these values :

Mmax = ( 6.5 *(1.5) * (1.5/2) + 4 * 1.5 ) = 13312.5 N.m

Vmax = ( 6.5 * (1.5) + 4 ) = 13750 N

A) determine max bending stress

б = \frac{MC}{I}  =  \frac{13312.5 ( 0.112)}{1/12(0.1^4-0.084^4)}  =  159.07 MPa

B) Determine max transverse shear stress

attached below

   ζ = 10.45 MPa

C) Determine max shear stress in the beam

This occurs at the top of the beam or at the centroidal axis

hence max stress in the beam =  159.07 / 2 = 79.535 MPa  

attached below is the remaining solution

6 0
3 years ago
Extra Credit: The Linc (parking lot and stadium)In celebration of the upcoming Super Bowl, for a maximum 10 points of extra cred
Sidana [21]

Answer:

Explanation:

// Below is the code to draw parking lot only , i have also put the o/p of code.

import java.util.*;

import java.lang.*;

import java.io.*;

public class Linc

{

static int size=4;

static int numBoxes = 1; // one row.

static int height =(int) Math.pow(size, 2); //Just how many | will it have.

static int width = 2; // two boxes in a row

public static void main (String[] args) throws java.lang.Exception

{

top();

printHeight();

}

  public static void top(){

  for(int i = 0; i <= numBoxes*width;i++)

  {

  if(i%width == 0) {//When this happens we're in a new box.

      System.out.print(" ");

      System.out.print("____________");

  }

  else

  System.out.print(" ");

  }

  System.out.println(); //Move to next line.

 

  }

  public static void printHeight(){

  for(int j = 0; j < height;j++){

  for(int i = 0; i <= numBoxes*width;i++)

  {

  if(i%width == 0) {

  System.out.print("|"); //Whenever this happens we're in a new box.

  System.out.print("____________");

     

  }

  else

  System.out.print(" ");

 

  }

  System.out.print("|");

  System.out.println(); //Move to next line.

  }

  }

}

8 0
3 years ago
Other questions:
  • A mixing basin in a sewage filtration plant is stirred by a mechanical agitator with a power input/WF L T=. Other parameters de
    8·1 answer
  • Consider an open loop 1-degree-of-freedom mass-spring damper system. The system has mass 4.2 kg, and spring stiffness of 85.9 N/
    7·1 answer
  • Water vapor at 10 MPa, 600°C enters a turbine operating at steady state with a volumetric flow rate of 0.36 m3/s and exits at 0.
    15·1 answer
  • One gram of Strontium-90 has an activity of 5.3 terabecquerels (TBq), what will be the activity of 1 microgram?
    8·1 answer
  • declare integer product declare integer number product = 0 do while product &lt; 100 display ""Type your number"" input number p
    8·1 answer
  • 70 POINTS!!!!Showing results for Select the correct images. James is planning on registering for a course in electrical engineer
    5·2 answers
  • Do you know anything about Android graphics?
    5·1 answer
  • Three natural materials that were used by early man as roof covering​
    11·2 answers
  • Does anyone know the answer to this
    5·2 answers
  • What are the inputs and outputs of a sailboat?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!