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
Sladkaya [172]
3 years ago
11

1. A priority queue is an abstract data type which is like a regular queue or some other data structures, but where additionally

each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority like scheduler. If two elements have the same priority, they are served according to their order in the queue.

Engineering
1 answer:
Anit [1.1K]3 years ago
6 0

Answer:

The code is as below whereas the output is attached herewith

Explanation:

<em>package brainly.priorityQueue;</em>

<em>class PriorityJobQueue {</em>

<em>    Job[] arr;</em>

<em>    int size;</em>

<em>    int count;</em>

<em>    PriorityJobQueue(int size){</em>

<em>        this.size = size;</em>

<em>        arr = new Job[size];</em>

<em>        count = 0;</em>

<em>    }</em>

<em>    // Function to insert an element into the priority queue</em>

<em>    void insert(Job value){</em>

<em>        if(count == size){</em>

<em>            System.out.println("Cannot insert the key");</em>

<em>            return;</em>

<em>        }</em>

<em>        arr[count++] = value;</em>

<em>        heapifyUpwards(count);</em>

<em>    }</em>

<em>    // Function to heapify an element upwards</em>

<em>    void heapifyUpwards(int x){</em>

<em>        if(x<=0)</em>

<em>            return;</em>

<em>        int par = (x-1)/2;</em>

<em>        Job temp;</em>

<em>        if(arr[x-1].getPriority() < arr[par].getPriority()){</em>

<em>            temp = arr[par];</em>

<em>            arr[par] = arr[x-1];</em>

<em>            arr[x-1] = temp;</em>

<em>            heapifyUpwards(par+1);</em>

<em>        }</em>

<em>    }</em>

<em>    // Function to extract the minimum value from the priority queue</em>

<em>    Job extractMin(){</em>

<em>        Job rvalue = null;</em>

<em>        try {</em>

<em>            rvalue = arr[0].clone();</em>

<em>        } catch (CloneNotSupportedException e) {</em>

<em>            e.printStackTrace();</em>

<em>        }</em>

<em>        arr[0].setPriority(Integer.MAX_VALUE);</em>

<em>        heapifyDownwards(0);</em>

<em>        return rvalue;</em>

<em>    }</em>

<em>    // Function to heapify an element downwards</em>

<em>    void heapifyDownwards(int index){</em>

<em>        if(index >=arr.length)</em>

<em>            return;</em>

<em>        Job temp;</em>

<em>        int min = index;</em>

<em>        int left,right;</em>

<em>        left = 2*index;</em>

<em>        right = left+1;</em>

<em>        if(left<arr.length && arr[index].getPriority() > arr[left].getPriority()){</em>

<em>            min =left;</em>

<em>        }</em>

<em>        if(right <arr.length && arr[min].getPriority() > arr[right].getPriority()){</em>

<em>            min = right;</em>

<em>        }</em>

<em>        if(min!=index) {</em>

<em>            temp = arr[min];</em>

<em>            arr[min] = arr[index];</em>

<em>            arr[index] = temp;</em>

<em>            heapifyDownwards(min);</em>

<em>        }</em>

<em>    }</em>

<em>    // Function to implement the heapsort using priority queue</em>

<em>    static void heapSort(Job[] array){</em>

<em>        PriorityJobQueue object = new PriorityJobQueue(array.length);</em>

<em>        int i;</em>

<em>        for(i=0; i<array.length; i++){</em>

<em>            object.insert(array[i]);</em>

<em>        }</em>

<em>        for(i=0; i<array.length; i++){</em>

<em>            array[i] = object.extractMin();</em>

<em>        }</em>

<em>    }</em>

<em>}</em>

<em>package brainly.priorityQueue;</em>

<em>import java.io.BufferedReader;</em>

<em>import java.io.IOException;</em>

<em>import java.io.InputStreamReader;</em>

<em>import java.util.Arrays;</em>

<em>public class PriorityJobQueueTest {</em>

<em>    // Function to read user input</em>

<em>    public static void main(String[] args) {</em>

<em>        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));</em>

<em>        int n;</em>

<em>        System.out.println("Enter the number of elements in the array");</em>

<em>        try{</em>

<em>            n = Integer.parseInt(br.readLine());</em>

<em>        }catch (IOException e){</em>

<em>            System.out.println("An error occurred");</em>

<em>            return;</em>

<em>        }</em>

<em>        System.out.println("Enter array elements");</em>

<em>        Job[] array = new Job[n];</em>

<em>        int i;</em>

<em>        for(i=0; i<array.length; i++){</em>

<em>            Job job  =new Job();</em>

<em>            try{</em>

<em>                job.setJobId(i);</em>

<em>                System.out.println("Element "+i +"priority:");</em>

<em>                job.setJobName("Name"+i);</em>

<em>                job.setSubmitterName("SubmitterName"+i);</em>

<em>                job.setPriority(Integer.parseInt(br.readLine()));</em>

<em>                array[i] = job;</em>

<em>            }catch (IOException e){</em>

<em>                System.out.println("An error occurred");</em>

<em>            }</em>

<em>        }</em>

<em>        System.out.println("The initial array is");</em>

<em>        System.out.println(Arrays.toString(array));</em>

<em>        PriorityJobQueue.heapSort(array);</em>

<em>        System.out.println("The sorted array is");</em>

<em>        System.out.println(Arrays.toString(array));</em>

<em>        Job[] readyQueue =new Job[4];</em>

<em>    }</em>

<em>}</em>

You might be interested in
Exercises
Feliz [49]

Answer:

Rocket

Gas

Explanation:

5 0
3 years ago
A machine raises 20kg of water through a height of 50m in 10secs. What is the power of the machine.​
Tomtit [17]

Answer:

hhahhhwghwhwhwhwjwnwjnnnnwnwwnw

Explanation:

jwkwkkwoiwiwiwiwiwowwiwowowiiiiwuuwuwgeevehehsvhsvwhbhhehehwgjjwhwhjwjqwjjuuuwi####!\\\\e

5 0
3 years ago
For the following circuit diagram, if A=010 , B= 101.
Fantom [35]

Answer:

cgghhhh chick jjkkkkkki

4 0
3 years ago
In order to fill a tank of 1000 liter volume to a pressure of 10 atm at 298K, an 11.5Kg of the gas is required. How many moles o
lesya [120]

Answer:

The molecular weight will be "28.12 g/mol".

Explanation:

The given values are:

Pressure,

P = 10 atm

  = 10\times 101325 \ Pa

  = 1013250 \ Pa

Temperature,

T = 298 K

Mass,

m = 11.5 Kg

Volume,

V = 1000 r

   = 1 \ m^3

R = 8.3145 J/mol K

Now,

By using the ideal gas law, we get

⇒ PV=nRT

o,

⇒ n=\frac{PV}{RT}

By substituting the values, we get

       =\frac{1013250\times 1}{8.3145\times 298}

       =408.94 \ moles

As we know,

⇒ Moles(n)=\frac{Mass(m)}{Molecular \ weight(MW)}

or,

⇒        MW=\frac{m}{n}

                   =\frac{11.5}{408.94}

                   =0.02812 \ Kg/mol

                   =28.12 \ g/mol

3 0
3 years ago
Water vapor at 10bar, 360°C enters a turbine operatingat steady state with a volumetric flow rate of 0.8m3/s and expandsadiabati
Artyom0805 [142]

Answer:

A) W' = 178.568 KW

B) ΔS = 2.6367 KW/k

C) η = 0.3

Explanation:

We are given;

Temperature at state 1;T1 = 360 °C

Temperature at state 2;T2 = 160 °C

Pressure at state 1;P1 = 10 bar

Pressure at State 2;P2 = 1 bar

Volumetric flow rate;V' = 0.8 m³/s

A) From table A-6 attached and by interpolation at temperature of 360°C and Pressure of 10 bar, we have;

Specific volume;v1 = 0.287322 m³/kg

Mass flow rate of water vapour at turbine is defined by the formula;

m' = V'/v1

So; m' = 0.8/0.287322

m' = 2.784 kg/s

Now, From table A-6 attached and by interpolation at state 1 with temperature of 360°C and Pressure of 10 bar, we have;

Specific enthalpy;h1 = 3179.46 KJ/kg

Now, From table A-6 attached and by interpolation at state 2 with temperature of 160°C and Pressure of 1 bar, we have;

Specific enthalpy;h2 = 3115.32 KJ/kg

Now, since stray heat transfer is neglected at turbine, we have;

-W' = m'[(h2 - h1) + ((V2)² - (V1)²)/2 + g(z2 - z1)]

Potential and kinetic energy can be neglected and so we have;

-W' = m'(h2 - h1)

Plugging in relevant values, the work of the turbine is;

W' = -2.784(3115.32 - 3179.46)

W' = 178.568 KW

B) Still From table A-6 attached and by interpolation at state 1 with temperature of 360°C and Pressure of 10 bar, we have;

Specific entropy: s1 = 7.3357 KJ/Kg.k

Still from table A-6 attached and by interpolation at state 2 with temperature of 160°C and Pressure of 1 bar, we have;

Specific entropy; s2 = 8.2828 KJ/kg.k

The amount of entropy produced is defined by;

ΔS = m'(s2 - s1)

ΔS = 2.784(8.2828 - 7.3357)

ΔS = 2.6367 KW/k

C) Still from table A-6 attached and by interpolation at state 2 with s2 = s2s = 8.2828 KJ/kg.k and Pressure of 1 bar, we have;

h2s = 2966.14 KJ/Kg

Energy equation for turbine at ideal process is defined as;

Q' - W' = m'[(h2 - h1) + ((V2)² - (V1)²)/2 + g(z2 - z1)]

Again, Potential and kinetic energy can be neglected and so we have;

-W' = m'(h2s - h1)

W' = -2.784(2966.14 - 3179.46)

W' = 593.88 KW

the isentropic turbine efficiency is defined as;

η = W_actual/W_ideal

η = 178.568/593.88 = 0.3

8 0
4 years ago
Other questions:
  • A converging-diverging nozzle is designed to operate with an exit Mach number of 1.75 . The nozzle is supplied from an air reser
    15·1 answer
  • The ratio of the weight of a substance to the weight of equal volume of water is known as a) Density b) specific gravity c) spec
    8·1 answer
  • A part made from annealed AISI 1018 steel undergoes a 20 percent cold-work operation. Obtain the yield strength and ultimate str
    10·1 answer
  • How many D-cell batteries would it take to power a human for 1 day?
    11·1 answer
  • python Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month an
    7·1 answer
  • Plateau Creek carries 5.0 m^3 /s of water with a selenium (Se) concentration of 0.0015 mg/L. A farmer withdraws water at a certa
    12·1 answer
  • Technician A says that the use of methanol in internal combustion engines has declined over the years. Technician B says that th
    10·1 answer
  • Can someone please help!
    8·1 answer
  • At least 1 sources must be from an organization website (.org) and 1 source must be from and educational website .edu) Do not us
    7·1 answer
  • Explain wet and dry compression tests​
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!