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
Impact strips may be designed into a bumper cover.<br> True<br> False
d1i1m1o1n [39]

Answer:

true I think

Explanation:

true I think

5 0
3 years ago
Suppose the loop is moving toward the solenoid (to the right). Will current flow through the loop down the front, up the front,
Tems11 [23]

Answer:

See explanation

Explanation:

The magnetic force is

F = qvB sin θ

We see that sin θ = 1, since the angle between the velocity and the direction of the field is 90º. Entering the other given quantities yields

F

=

(

20

×

10

−

9

C

)

(

10

m/s

)

(

5

×

10

−

5

T

)

=

1

×

10

−

11

(

C

⋅

m/s

)

(

N

C

⋅

m/s

)

=

1

×

10

−

11

N

6 0
3 years ago
Read 2 more answers
Does the Diesel engine have engine knock or detonation problem? Why?
Luda [366]

Explanation:

Yes Diesel engine have problem of knocking.

We know that knocking is phenomenon in which suddenly large amount of power generates this large amount of power will cause the failure of diesel engine.

Actually when one set of fuel inject inside the cylinder to burn with already compressed air (in general up to 10-15 bar) then this fuel does not burn complete and accumulate inside the cylinder.After that second set of fuel inject inside the cylinder then that one set of fuel burns with second set of fuel and produces large amount of sudden power for engine and causes the breaks in the crank or connecting rod of engine.it leads to damage the engine.

6 0
3 years ago
Of the cost reduction strategies for workers' compensation mentioned in the required readings, which one do you think would work
Vesnalui [34]

In industries together with production, we want people to address the manufacturing of merchandise and the usage of heavy machinery.

<h3>What is the painting situation?</h3>

In such painting situations, people are at risk of injuries, and this prices the maximum for the company. So so that you can put into effect value discount is such conditions we want to have right coincidence cowl plans for the people and make sure all of the protection precautions are taken withinside the factory.

  1. The people have to be properly educated on using protection measures and in case any injuries arise we have to have coverage claims in order that we not want to make investments extra cash and we also can offer protection and protection to the people.
  2. This approach is excellent for this enterprise due to the fact regardless of what number of precautions we take people are uncovered to fitness risks and as a result having the right coverage insurance is a superb value discount strategy.

Read more bout the compensation :

brainly.com/question/25273589

#SPJ1

3 0
2 years ago
Design a sequential circuit DETECTOR that has one input X and one input Y. The DETECTOR detects the sequence 110. If an input X
Novosadov [1.4K]

Answer:

See explaination

Explanation:

This is going to require diagrams, please kindly see attachment for the detailed step by step solution of the given problem.

5 0
3 years ago
Other questions:
  • The flowchart below shows the design steps required to build a working model.
    6·1 answer
  • A socket can be driven using any of the following except for (A) a socket ratchet
    14·1 answer
  • 7. The "3 second rule" is the time you should pause at an intersection marked with a stop sign.
    6·1 answer
  • 7. Sockets internal designs come in what sizes?
    5·1 answer
  • A three-point bending test is performed on a glass specimen having a rectangular cross section of height 5.3 mm and width 11.6 m
    11·1 answer
  • Electricians will sometimes call ______ "disconnects" or a "disconnecting means."
    15·1 answer
  • If an elevator repairer observes that cables begin to fray after 15 years, what process might he or she use to create a maintena
    11·1 answer
  • Is the science of measurement
    13·2 answers
  • A ruptured desiccant bag in a reciever-driver is usually caused by what?​
    13·1 answer
  • Javier’s class visited a power plant near his city, and they learned how it produced electricity. What does this form of power d
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!