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
A particular motor rotates at 3000 revolutions per minute. What is its speed in rad/sec, and how many seconds does it takes to m
Leno4ka [110]

Answer:

ω=314.15 rad/s.

0.02 s.

Explanation:

Given that

Motor speed ,N= 3000 revolutions per minute

N= 3000 RPM

The speed of the motor in rad/s given as

\omega=\dfrac{2\pi N}{60}\ rad/s

Now by putting the values in the above equation

\omega=\dfrac{2\pi \times 3000}{60}\ rad/s

ω=314.15 rad/s

Therefore the speed in rad/s will be 314.15 rad/s.

The speed in rev/sec given as

\omega=\dfrac{ 3000}{60}\ rad/s

ω= 50 rev/s

It take 1 sec to cover 50 revolutions

That is why to cover 1 revolution it take

\dfrac{1}{50}=0.02\ s

4 0
2 years ago
For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle
Alex Ar [27]

Answer:

Java program is given below. You can get .class after you execute java programs, You can attach those files along with .java classes given , Those .class files are generated ones.

Explanation:

//Rectangle.java class

public class Rectangle {

private int x;

private int y;

private int width;

private int height;

// constructs a new Rectangle with the given x,y, width, and height

public Rectangle(int x, int y, int w, int h)

{

this.x=x;

this.y=y;

this.width=w;

this.height=h;

}

// returns the fields' values

public int getX()

{

return x;

}

public int getY()

{

return y;

}

public int getWidth()

{

return width;

}

public int getHeight()

{

return height;

}

// returns a string such as “Coordinate is (5,12) and dimension is 4x8” where 4 is width and 8 is height. public String toString()

public String toString()

{

String str="";

//add x coordidate , y-coordinate , width, height and area to str and return

str+="Coordinate is ("+x+","+y+")";

str+=" and dimension is : "+width+"x"+height;

str+=" Area is "+(width*height);

return str;

}

public void changeSize(int w,int h)

{

width=w;

height=h;

}

}

======================

//main.java

class Main {

public static void main(String[] args) {

//System.out.println("Hello world!");

//create an object of class Rectangle

Rectangle rect=new Rectangle(5,12,4,8);

//print info of rect using toString method

System.out.println(rect.toString());

//chamge width and height

rect.changeSize(3,10);

//print info of rect using toString method

System.out.println(rect.toString());

}

}

==========================================================================================

//Output

Coordinate is (5,12) and dimension is : 4x8 Area is 32

Coordinate is (5,12) and dimension is : 3x10 Area is 30

========================================================================================

6 0
3 years ago
In DC electrode positive, how much power is at the work clamp?
Korolek [52]

Answer:

1/3 power

Explanation:

I'm just a smart guy

7 0
2 years ago
2. The moist weight of 0.1 ft3 of soil is 12.2 lb. If the moisture content is 12% and the specific gravity of soil solids is 2.7
adell [148]

The answers to dry unit weight, void ratio, porosity, degree of saturation, volume occupied by water are respectively;

γ_d = 108.93 lb/ft³; e = 0.56; n = 0.36; S = 0.58; V_w = 0.021 ft³

<h3>Calculation of Volume and Weight of soil</h3>

We are given;

Moist weight; W = 12.2 lb

Volume of moist soil; V = 0.1 ft³

moisture content; w = 12% = 0.12

Specific gravity of soil solids; G_s = 2.72

A) Formula for dry unit weight is;

γ_d = γ/(1 + w)

where γ_w is moist unit weight as;

γ_w = W/V

γ_w = 122/0.1 = 122 lb/ft³

Thus;

γ_d = 122/(1 + 0.12)

γ_d = 108.93 lb/ft³

B) Formula for void ratio is;

e = [(G_s * γ_w)/γ_d] - 1

e = [(2.72 * 122)/108.93] - 1

e = 0.56

C) Formula for porosity is;

n = e/(1 + e)

n = 0.56/(1 + 0.56)

n = 0.36

D) Formula for degree of saturation is;

S = (w * G_s)/e

S = (0.12 * 2.72)/0.56

S = 0.58

E) Volume occupied by water is gotten from;

V_w = S*V_v

where;

V_v is volume of voids = nV

V_v = 0.36*0.1

V_v = 0.036 ft³

Thus;

V_w = 0.58 * 0.036

V_w = 0.021 ft³

Read more about Specific Gravity of Soil at; brainly.com/question/14932758

4 0
2 years ago
(d) Arches NP is known for its spectacular arches that develop in the jointed areas of the park. Placemark Problem 2d flies you
Whitepunk [10]

Answer:

☐ NE-SW

Explanation:

Based on the description, the rock direction is North East - South West (NE-SW). Rocks generally can expand or compress depending on the type and magnitude of stress applied on the rocks. However, if the applied stress is sufficiently high, cracks and fractures will be created on the rock and it can ultimately lead to the formation of particles.

8 0
3 years ago
Other questions:
  • The temperature of a flowing gas is to be measured with a thermocouple junction and wire stretched between two legs of a sting,
    8·1 answer
  • Q10: Technician A says that nearly all brands of scan tools will pull DTCs from the ABS
    14·1 answer
  • Define an ADT for a two-dimensional array of integers. Specify precisely the basic operations that can be performed on such arra
    5·1 answer
  • 50.38
    14·1 answer
  • A well penetrates an unconfined aquifer. Prior to pumping, the water level (head) is 25 meters. After a long period of pumping a
    14·1 answer
  • What is the t max for a carbon steel heater tube?
    8·1 answer
  • Calculate pressure at the mid-plane of an annular cylinder of iron powder pressed using double-action press. The punch pressure
    12·1 answer
  • Sarah needs to create an architectural drawing for a museum building with an inclined surface. Which presentation view will be t
    15·1 answer
  • What is the most important part of a successful Election Day?
    10·2 answers
  • Is an example of an electrical device.
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!