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
What is the resistance of a resistor if the current flowing through it is 3mA and the voltage across it is 5.3V?
Flura [38]

Answer: 1766.667 Ω = 1.767kΩ

Explanation:

V=iR

where V is voltage in Volts (V), i is current in Amps (A), and R is resistance in Ohms(Ω).

3mA = 0.003 A

Rearranging the equation, we get

R=V/i

Now we are solving for resistance. Plug in 0.003 A and 5.3 V.

R = 5.3 / 0.003

= 1766.6667 Ω

= 1.7666667 kΩ

The 6s are repeating so round off to whichever value you need for exactness.

6 0
1 year ago
Determine whether or not each of the following signals is periodic.
Sloan [31]

Answer:

a) periodic (N = 1)

b) not periodic

c) not periodic

d) periodic (N = 8)

e) periodic (N = 16)

Explanation:

For function to be a periodic: f(n) = f(n+N)

a) x[n]=sin(\frac{8\pi}{2}n+1)\\\\sin(\frac{8\pi}{2}n+1)=sin(4\pi n+1)

It is periodic with fundamental period N = 1

b) x[n]=cos(\frac{n}{8} -\pi)\\\\\frac{1}{8} N=2\pi k

N must be integer. So it is nor periodic

c) x[n]=cos(\frac{\pi}{8} n^2)\\\\cos(\frac{\pi}{8} (n+N)^2)=cos(\frac{\pi}{8} (n^2+N^2+2nN)\\\\N^2 = 16 \:\:or\:\:2nN=16

Since N is dependent to n. So it is not periodic.

d) x[n]=cos(\frac{\pi }{2}  n) cos(\frac{\pi }{4}  n)\\\\x[n] = \frac{1}{2} cos(\frac{3\pi }{4} n) + \frac{1}{2} cos(\frac{\pi }{4} n)\\\\N_1=8\:\:and\:\:N_2=8\\

So it is periodic with fundamental period N = 8.

e) x[n]=2cos(\frac{\pi }{4}  n)+sin(\frac{\pi }{8} n)-2cos(\frac{\pi }{2} n+\frac{\pi }{6} )\\\\N_1=8\:\:and\:\:N_2=16\:\:and\:\:N_3=4

So it is periodic with N = 16.

3 0
3 years ago
Indicate whether the following statements are true or false for an isothermal process: (A) Q=T(∆S). (B) ∆U=0.(C) The entropy cha
Lena [83]

Answer:

A=False

B=False

C=False

D=False

E=False

F=False

Explanation:

A. In an isothermal process, only the reversibly heat transfer is 0, Q_{rev}=T (\Delta S)

B. Consider the phase change of boiling water. Here, the temperature remains constant but the internal energy of the system increases.

C. This is not true even in reversible process, as can be inferred from the equation in part A.

D. This is only true in reversible processes, but not in all isothermal processes.

E. Consider the phase change of freezing water. Here, the surroundings are increasing their entropy, as they are taking in heat from the system.

F. This is not true if (\Delta U)\neq 0, like in answer B. One case where this is true is in the reversible isothermal expansion (or compression) of an ideal gas.

3 0
3 years ago
How do heat and our use of electricity affect our daily activities and the environment
Andrews [41]

Answer:

there hope it can help.......

3 0
2 years ago
Jnjn freeeeeeeeeeeeeeeeeeeeeeeeeeeeeee pointtttttttttt
kvasek [131]

Answer:

thx

Explanation:

5 0
3 years ago
Read 2 more answers
Other questions:
  • The format_address function separates out parts of the address string into new strings: house_number and street_name, and return
    8·1 answer
  • A evolução da malha rodoviária do Brasil é um marco notável
    9·1 answer
  • 1. (16 points) True or False, one point each, Write down F (false) or T (true). ___ (01) In a mechanical design, it is recommend
    12·1 answer
  • Paragraph summary on airplane history
    8·1 answer
  • A town is designing a rectangular, 4m deep settling tank for treating surface water intake. The tank will have a flow velocity o
    14·1 answer
  • Determine if the fluid is satisfied​
    10·1 answer
  • Does anyone know obamas last name???? please help its for a friend I swear!!!111!!11!
    8·1 answer
  • Plz watch our you tube channel called addie nahoe. I got 8 subscribers. I need 10. Plz like and hit that nocation bell. Plz!!!!
    12·1 answer
  • How long would it take a marble to travel down a 15 inch piece of cardboard at a 15 degree angle?
    12·1 answer
  • Describe how you would control employee exposure to excessive noise in a mining environment
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!