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
30POINTS
garri49 [273]
Concentrating solar power (CSP) plants use mirrors to concentrate the sun's energy to drive traditional steam turbines or engines that create electricity. The thermal energy concentrated in a CSP plant can be stored and used to produce electricity when it is needed, day or night. Today, roughly 1,815 megawatts (MWac) of CSP plants are in operation in the United States.

Parabolic Trough
Parabolic trough systems use curved mirrors to focus the sun’s energy onto a receiver tube that runs down the center of a trough. In the receiver tube, a high-temperature heat transfer fluid (such as a synthetic oil) absorbs the sun’s energy, reaching temperatures of 750°F or higher, and passes through a heat exchanger to heat water and produce steam. The steam drives a conventional steam turbine power system to generate electricity. A typical solar collector field contains hundreds of parallel rows of troughs connected as a series of loops, which are placed on a north-south axis so the troughs can track the sun from east to west. Individual collector modules are typically 15-20 feet tall and 300-450 feet long.

Compact Linear Fresnel Reflector
CLFR uses the principles of curved-mirror trough systems, but with long parallel rows of lower-cost flat mirrors. These modular reflectors focus the sun's energy onto elevated receivers, which consist of a system of tubes through which water flows. The concentrated sunlight boils the water, generating high-pressure steam for direct use in power generation and industrial steam applications.
3 0
3 years ago
Read 2 more answers
Select the right answer<br>​
Kruka [31]

Answer:

for 1st question the answer is 5th option.

for 2nd question the answer is 2nd option

hope it helps you mate

please mark me as brainliast

5 0
3 years ago
A glass tube is inserted into a flowing stream of water with one opening directed upstream and the other end vertical. If the wa
Furkat [3]

Answer:

h=0.46m

Explanation:

From the question we are told that:

Velocity of water V=3m/s

Height=?

Generally, the equation for Water Velocity is mathematically given by

V=\sqrt{2gh}

Therefore Height h is given as

h=\frac{v}{2g}

h=\frac{3^2}{2*9.81}

h=0.46m

5 0
3 years ago
What is the objective of phasing out an INDUCTION MOTOR before putting the machine into commission?
enyata [817]

The main objective of phasing out an INDUCTION MOTOR is to identify the ends of the stator coils.

<h3>What is an induction motor?</h3>

An induction motor is a device based on alternate electricity (AC) which is composed of three different stator coils.

An induction motor is a device also known as an asynchronous motor due to its irregular velocity.

In conclusion, the objective of phasing out an INDUCTION MOTOR is to identify the ends of the stator coils.

Learn more on induction motors here:

brainly.com/question/15721280

#SPJ1

8 0
2 years ago
The best way to identify common masonry problems is to call the engineer.<br> True or False
Daniel [21]

Answer:

True

Explanation:

5 0
3 years ago
Other questions:
  • The small washer is sliding down the cord OA. When it is at the midpoint, its speed is 28 m/s and its acceleration is 7 m/s 2 .
    13·1 answer
  • A photovoltaic panel of dimension 2m×4m is installed on the
    14·1 answer
  • Which of the following statements define drug abuse
    7·1 answer
  • Sadadasdasdasdasdadaaasd1
    14·1 answer
  • Which type of load is not resisted by a pinned joint? A) Moment B) Shear C) Axial D) Compression
    7·1 answer
  • A 220-V electric heater has two heating coils that can be switched such that either coil can be used independently or the two ca
    15·1 answer
  • Water enters an ice machine at 55°F and leaves as ice at 25°F. If the COP of the ice machine is 2.45 during this operation, dete
    7·1 answer
  • Please please help please with this this is the link for the story PLEASE PLEASE HELP PLEASE PLEASE help please
    7·1 answer
  • I need help due today please help
    5·1 answer
  • Need help, I will give cake :))<br><br> + branliest
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!