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]
4 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]4 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
Why do we care about a material's ability to resist torsional deformation?
lesya692 [45]

Answer:

(A) Because the angle of twist of a material is often used to predict its shear toughness

Explanation:

In engineering, torsion is the solicitation that occurs when a moment is applied on the longitudinal axis of a construction element or mechanical prism, such as axes or, in general, elements where one dimension predominates over the other two, although it is possible to find it in diverse situations.

The torsion is characterized geometrically because any curve parallel to the axis of the piece is no longer contained in the plane initially formed by the two curves. Instead, a curve parallel to the axis is twisted around it.

The general study of torsion is complicated because under that type of solicitation the cross section of a piece in general is characterized by two phenomena:

1- Tangential tensions appear parallel to the cross section.

2- When the previous tensions are not properly distributed, which always happens unless the section has circular symmetry, sectional warps appear that make the deformed cross sections not flat.

5 0
3 years ago
Describe the greatest power in design according to Aravena?
Ann [662]

Answer: Describe the greatest power in design according to Aravena? The subject of Aravena’s recent Futuna Lecture Series in New Zealand was ‘the power of design,’ which he described as ultimately being “the power of synthesis” because, increasingly, architects are dealing with complex issues and problems.

What are the three problems with global urbanization? 1. Degraded Environmental Quality ...

2. Overcrowding ...

3. Housing Problems ...

4. Unemployment ...

5. Development of Slums...

How could you use synthesis in your life to solve problems? Hence, synthesis is often not a one-time process of solution design but is used in combination with problem understanding and solution analysis to progress towards a more complete understanding of problems and solutions over time (see Applying the Systems Approach topic for a more complete discussion of the dynamics of this aspect of the approach).

I got all three answers

4 0
2 years ago
Which examples best demonstrate likely tasks for Health, Safety, and Environmental Assurance workers? check all that apply
Over [174]

Sam, Elijah, Joy Those are 100% correct from my human knowledge

6 0
3 years ago
Read 2 more answers
The common type of defects found when soldering on a printed circuit board
sammy [17]

Explanation:

Solder Bridges

Plating Voids

Non-wetting or dewetting.

5 0
3 years ago
An adiabatic air compressor compresses 10 L/s of air at 120 kPa and 20 degree C to 1000 kPa and 300 degree C.
Oksana_A [137]

Answer:

work=281.4KJ/kg

Power=4Kw

Explanation:

Hi!

To solve follow the steps below!

1. Find the density of the air at the entrance using the equation for ideal gases

density=\frac{P}{RT}

where

P=pressure=120kPa

T=20C=293k

R= 0.287 kJ/(kg*K)= gas constant ideal for air

density=\frac{120}{(0.287)(293)}=1.43kg/m^3

2.find the mass flow by finding the product between the flow rate and the density

m=(density)(flow rate)

flow rate=10L/s=0.01m^3/s

m=(1.43kg/m^3)(0.01m^3/s)=0.0143kg/s

3. Please use the equation the first law of thermodynamics that states that the energy that enters is the same as the one that must come out, we infer the following equation, note = remember that power is the product of work and mass flow

Work

w=Cp(T1-T2)

Where

Cp= specific heat for air=1.005KJ/kgK

w=work

T1=inlet temperature=20C

T2=outlet temperature=300C

w=1.005(300-20)=281.4KJ/kg

Power

W=mw

W=(0.0143)(281.4KJ/kg)=4Kw

5 0
3 years ago
Other questions:
  • Amanda and Tyler opened a business that specializes in shipping liquids, such as milk, juice, and water, in cylindrical containe
    5·1 answer
  • Air is compressed in a piston-cylinder device. List three examples of irreversibilities that could occur
    13·1 answer
  • A 200 W vacuum cleaner is powered by an electric motor whose efficiency is 70%. (Note that the electric motor delivers 200 W of
    13·1 answer
  • he ventilating fan of the bathroom of a building has a volume flow rate of 28 L/s and runs continuously. If the density of air i
    9·1 answer
  • A(n) is a detailed, structured diagram or drawing.
    6·1 answer
  • Add my sc please?.<br><br> kindacracked
    12·2 answers
  • Determine the wattmeter reading when it is connected to resistor load.​
    11·1 answer
  • A steam turbine receives steam at 1.5MPa and 220oC, and exhausts at 50kPa, 0.75 dry. Neglecting heat losses and changes in kinet
    6·1 answer
  • 'Energy' has the potential to:
    6·1 answer
  • 8th grade safety test
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!