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
Here, we want to become proficient at changing units so that we can perform calculations as needed. The basic heat transfer equa
netineya [11]

Answer:

9500 kJ; 9000 Btu

Explanation:

Data:

m = 100 lb

T₁ = 25 °C

T₂ = 75 °C

Calculations:

1. Energy in kilojoules

ΔT = 75 °C - 25 °C = 50 °C  = 50 K

m = \text{100 lb} \times \dfrac{\text{1 kg}}{\text{2.205 lb}} \times \dfrac{\text{1000 g}}{\text{1 kg}}= 4.54 \times 10^{4}\text{ g}\\\\\begin{array}{rcl}q & = & mC_{\text{p}}\Delta T\\& = & 4.54 \times 10^{4}\text{ g} \times 4.18 \text{ J$\cdot$K$^{-1}$g$^{-1}$} \times 50 \text{ K}\\ & = & 9.5 \times 10^{6}\text{ J}\\ & = & \textbf{9500 kJ}\\\end{array}

2. Energy in British thermal units

\text{Energy} = \text{9500 kJ} \times \dfrac{\text{1 Btu}}{\text{1.055 kJ}} = \text{9000 Btu}

7 0
3 years ago
A genetically engineered hormone.
I am Lyosha [343]
What is the question?
3 0
3 years ago
In part A you are asked to write the pseudocode for the program. In part B you are asked to write the syntax of the code for the
Naya [18.7K]

Answer:

C++.

Explanation:

#include <iostream>

#include <string>

using namespace std;

///////////////////////////////////////////////////////////////

int main() {

   string quote, book;

   int page;

   

   cout<<"What is your favorite quote from a book?"<<endl;

   getline(cin, quote);

   cout<<endl;

   /////////////////////////////////////////////

   cout<<"What book was that quote from?"<<endl;

   getline(cin, book);

   cout<<endl;

   /////////////////////////////////////////////

   cout<<"What page was that quote from?"<<endl;

   cin>>page;

   cout<<endl;

   /////////////////////////////////////////////

   int no_of_upper_characters = 0;

   for (int i=0; i<quote.length(); i++) {

       if (isupper(quote[i]))

          no_of_upper_characters++;

   }

   

   cout<<"No. of upper case characters: "<<no_of_upper_characters<<endl;

   /////////////////////////////////////////////

   int no_of_characters = quote.length();

   cout<<"No. of characters: "<<no_of_characters<<endl;

   /////////////////////////////////////////////

   bool isDog = false;

   for (int i=0; i<quote.length(); i++) {

       if (isDog == true)

           break;

       else if (quote[i] == 'd') {

           for (int j=i+1; j<quote.length(); j++) {

               if (isDog == true)

                   break;

               else if (quote[j] == 'o') {

                   for (int z=j+1; z<quote.length(); z++) {

                       if (quote[z] == 'g') {

                           isDog = true;

                           break;

                       }

                   }

               }

           }

       }

   }

   

   if (isDog == true)

       cout<<"This includes 'd' 'o' 'g' in the quote";

   //////////////////////////////////////////////

   return 0;

}

3 0
3 years ago
Its been days wsgggggg
Nataly [62]

Answer: ok

Explanation:

4 0
2 years ago
Read 2 more answers
While discussing VIN numbers, Technician A says that the first digit of the VIN identifies the country where the vehicle was man
ruslelena [56]
Usually the first digit of the vin id’s the country it was built. So technician A would be correct. That’s usually how it is. Hope this helps. Please let me know if this is incorrect
4 0
3 years ago
Other questions:
  • why HF (hydrogen fluoride) has higher boiling temperature than HCl (hydrogen chloride), even thought HF has lower molecular weig
    8·1 answer
  • Steam enters a turbine at 120 bar, 508oC. At the exit, the pressure and quality are 50 kPa and 0.912, respectively. Determine th
    5·1 answer
  • The air velocity in the duct of a heating system is to be measured by a Pitot-static probe inserted into the duct parallel to th
    13·1 answer
  • Suppose there are 76 packets entering a queue at the same time. Each packet is of size 5 MiB. The link transmission rate is 2.1
    5·1 answer
  • Shear plane angle and shear strain: In an orthogonal cutting operation, the tool has a rake angle = 16°. The chip thickness befo
    7·1 answer
  • At the inlet to the combustor of a supersonic combustion ramjet (or scramjet), the flow Mach number is supersonic. For a fuel-ai
    12·1 answer
  • How to design a solar panel<br>​
    7·1 answer
  • The map shows the distribution of dairy farms across the lower 48 of the United States. Each dot on the map represents approxima
    11·1 answer
  • When you come to an intersection, follow the _________ before you proceed.
    6·2 answers
  • Pleae answer brainlest due today
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!