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
lara31 [8.8K]
3 years ago
9

For this problem, you may not look at any other code or pseudo-code (even if it is on the internet), other than what is on our w

ebsite or in our book. You may discuss general ideas with other people. Assume A[1. . . n] is a heap, except that the element at index i might be too large. For the following parts, you should create a method that inputs A, n, and i, and makes A into a heap.
Engineering
1 answer:
sergiy2304 [10]3 years ago
8 0

Answer:

(a)

(i) pseudo code :-

current = i

// assuming parent of root is -1

while A[parent] < A[current] && parent != -1 do,

if A[parent] < A[current] // if current element is bigger than parent then shift it up

swap(A[current],A[parent])

current = parent

(ii) In heap we create a complete binary tree which has height of log(n). In shift up we will take maximum steps equal to the height of tree so number of comparison will be in term of O(log(n))

(b)

(i) There are two cases while comparing with grandparent. If grandparent is less than current node then surely parent node also will be less than current node so swap current node with parent and then swap parent node with grandparent.

If above condition is not true then we will check for parent node and if it is less than current node then swap these.

pseudo code :-

current = i

// assuming parent of root is -1

parent is parent node of current node

while A[parent] < A[current] && parent != -1 do,

if A[grandparent] < A[current] // if current element is bigger than parent then shift it up

swap(A[current],A[parent])

swap(A[grandparent],A[parent])

current = grandparent

else if A[parent] < A[current]

swap(A[parent],A[current])

current = parent

(ii) Here we are skipping the one level so max we can make our comparison half from last approach, that would be (height/2)

so order would be log(n)/2

(iii) C++ code :-

#include<bits/stdc++.h>

using namespace std;

// function to return index of parent node

int parent(int i)

{

if(i == 0)

return -1;

return (i-1)/2;

}

// function to return index of grandparent node

int grandparent(int i)

{

int p = parent(i);

if(p == -1)

return -1;

else

return parent(p);

}

void shift_up(int A[], int n, int ind)

{

int curr = ind-1; // because array is 0-indexed

while(parent(curr) != -1 && A[parent(curr)] < A[curr])

{

int g = grandparent(curr);

int p = parent(curr);

if(g != -1 && A[g] < A[curr])

{

swap(A[curr],A[p]);

swap(A[p],A[g]);

curr = g;

}

else if(A[p] < A[curr])

{

swap(A[p],A[curr]);

curr = p;

}

}

}

int main()

{

int n;

cout<<"enter the number of elements :-\n";

cin>>n;

int A[n];

cout<<"enter the elements of array :-\n";

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

cin>>A[i];

int ind;

cout<<"enter the index value :- \n";

cin>>ind;

shift_up(A,n,ind);

cout<<"array after shift up :-\n";

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

cout<<A[i]<<" ";

cout<<endl;

}

Explanation:

You might be interested in
Come and look on my attachment​
CaHeK987 [17]

Crazy Guy what do uh mean ?

4 0
3 years ago
What is the area enclosed by the cycle area of the Carnot cycle illustrating on a P-V diagram?
Inga [223]

Answer:

The work of the cycle.

Explanation:

The area enclosed by the cycle of the Pressure-Volume diagram of a Carnot engine represents the net work performed by the cycle.

The expansions yield work, and this is represented by the area under the curve all the way to the p=0 line. But the compressions consume work (or add negative work) and this is substracted fro the total work. Therefore the areas under the compressions are eliminated and you are left with only the enclosed area.

7 0
3 years ago
96/64 reduced to its lowest term
Marina CMI [18]

Answer:

3/2

Explanation:

8 0
3 years ago
A battery is connected to a resistor. Increasing the resistance of the resistor will __________. A battery is connected to a res
belka [17]

Answer: the increase in the external resistor will affect and decrease the current in the circuit.

Explanation: A battery has it own internal resistance, r, and given an external resistor of resistance, R, the equation of typical of Ohm's law giving the flow of current is

E = IR + Ir = I(R + r)........(1)

Where IR is the potential difference flowing in the external circuit and Or is the lost voltage due to internal resistance of battery. From (1)

I = E/(R + r)

As R increases, and E, r remain constant, the value (R + r) increases, hence the value of current, I, in the external circuit decreases.

8 0
3 years ago
In Example 2-1, part c, the data were represented by the normal distribution function f(x)=0.178 exp(-0.100(x-451)2 Use this dis
valkas [14]

Answer:

P ( 2.5 < X < 7.5 ) = 0.7251

Explanation:

Given:

- The pmf for normal distribution for random variable x is given:

                                      f(x)=0.178 exp(-0.100(x-4.51)^2)

Find:

the fraction of individuals demonstrating a response in the range of 2.5 to 7.5.

Solution:

- The random variable X follows a normal distribution with mean u = 4.51, and standard deviation s.d as follows:

                               s.d = sqrt ( 1 / 0.1*2)

                               s.d = sqrt(5) =2.236067

- Hence, the normal distribution is as follows:

                               X ~ N(4.51 , 2.236)

- Compute the Z-score values of the end points 2.5 and 7.5:

              P ( (2.5 - 4.51) / 2.236 < Z < (7.5 - 4.51 ) / 2.236 )

              P ( -0.898899327 < Z < 1.337168651 )  

- Use the Z-Table for the probability required:

              P ( 2.5 < X < 7.5 ) = P ( -0.898899327 < Z < 1.337168651 ) = 0.7251            

6 0
3 years ago
Other questions:
  • A furnace wall composed of 200 mm, of fire brick. 120 mm common brick 50mm 80% magnesia and 3mm of steel plate on the outside. I
    13·1 answer
  • The inner surface of a hollow cylinder is subjected to tangential and axial stresses of 40,000 and 24,000 psi, respectively. Det
    9·1 answer
  • What are the different branches of engineering involved in manufacturing a general-purpose elevator?
    6·1 answer
  • Please help will give brainliest please answer all 3
    11·1 answer
  • Two loads connected in parallel draw a total of 2.4 kW at 0.8 pf lagging from a 120-V rms, 60-Hz line. One load absorbs 1.5 kW a
    5·1 answer
  • 3. (20 points) Suppose we wish to search a linked list of length n, where each element contains a key k along with a hash value
    7·1 answer
  • The question belongs to Electrical Engineering (Linear System).
    11·1 answer
  • saan nag tungo si Aguinaldo at ilang pinuno ng kilusan pagkatapos mapairal ang kasunduan na pansamantalang nag dulot ng kapayapa
    6·1 answer
  • Why do bridges collapse?
    9·2 answers
  • When adding two 8 bit binary numbers, which of the following statements is true?
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!