Question

• Implement the algorithm Build Heap in C (or C++). Algorithm buildHeap (heap, size) set walker to 1 loop (walker < size) reh

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ code

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;

//Creating a min Heap

void reheapUp(int arr[] , int index) {
       if(index==0)return;
       int parent=(index)/2;
       if(arr[index] < arr[parent]) {
           int temp=arr[index];
           arr[index]=arr[parent];
           arr[parent]=temp;
           reheapUp(arr , parent);
       }
   }
  
   void display(int arr[] , int size) {
       for(int i=0;i<size;i++) {
           cout<<arr[i]<<" ";
       }
  
   }
  
   void buildHeap(int arr[] , int size){
       int walker = 1;
       while(walker < size){
           reheapUp(arr , walker);
           walker++;
       }
   }
  
int main(){
   srand(time(NULL));
   int size = 30;
   int heap[size];
  
   for(int i=0;i<size;i++){
       heap[i] = rand()%100;
   }
  
   cout<<"Heap before build Heap\n";
   display(heap , size);
  
   buildHeap(heap ,size);
   cout<<"\n\nHeap after build Heap\n";
   display(heap , size);
  
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
• Implement the algorithm Build Heap in C (or C++). Algorithm buildHeap (heap, size) set walker...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement the algorithm Build Heap in C (or C++). Algorithm buildHeap (heap, size) set walker to...

    Implement the algorithm Build Heap in C (or C++). Algorithm buildHeap (heap, size) set walker to 1 loop (walker < size) reheapUp (heap, walker) increment walker end loop Write a program to test your program using a random integer array of size 30. Print the arrays before and after building the heap.

  • Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap...

    Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either > (in a max heap) or s (in a min heap) the key of C. The node at the "top" of the heap (with no parents) is called the root node. In binary-tree based heap, it...

  • What algorithm does this code perform please help out ASAP. You can add code and output...

    What algorithm does this code perform please help out ASAP. You can add code and output but I really need what algorithm it is performing 4. What algorithm does the following pseudocode perform? Declare Integer startscan Declare Integer minIndex Declare Integer minValue Declare Integer index For startScan = 0 To arraysize - 2 set minindex = startscan Set minValue = array [startscan] For index startscan 1 To arraySize 1 If array[index] < minValue Set minValue array [index] Set minIndex-index End...

  • 1. In Lab 4, you developed a program to build a Max Heap, and then Heap...

    1. In Lab 4, you developed a program to build a Max Heap, and then Heap Sort. Update the program by adding two additional functions: (a) AddData(A, N, V) where V is the new value added. (b) Delete a data Delete by giving the index of the data position Make sure to display the array after calling each of the function. 2. Write a program to implement Binary Search Algorithm, which will return the index of the data searched (V)....

  • in C please 20. Change the bubble sort algorithm (Program 12-5) as follows: Use two directional...

    in C please 20. Change the bubble sort algorithm (Program 12-5) as follows: Use two directional bubbling in each pass. In the first bubbling, the smallest ele. ment is bubbled up; in the second bubbling, the largest element is bubbled I down. This sort is known as the shaker sort. 12-5 Bubble Sort (continued) // Statements // Each iteration is one sort pass for (int current = 0, sorted = 0; current <= last && !sorted; current++) for (int walker...

  • 1. a) Describe an O(m)-time algorithm that, given a set of S of n distinct numbers...

    1. a) Describe an O(m)-time algorithm that, given a set of S of n distinct numbers and a positive integer k c n, determines the top k numbers in s b) Describe an O(n)-time algorithm that, given a set of S of n distinct numbers and a positive integer k < n, determines the smallest k numbers in S.

  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....

  • Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

    Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...

  • create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of...

    create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of integers and the array size. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. You must use pointers to work with the array. Hint: review pointers as parameters. b) Implement the function print_array that receives as parameters an array of integers and the array size. Use a for statements...

  • For c++ Write a complete C++ program and declare 2 arrays type double size 5. The...

    For c++ Write a complete C++ program and declare 2 arrays type double size 5. The name of the first array is Salary and the name of the second array is Tax. Use a for loop loop and enter 5 salaries type double into array Salary. Then multiply each element of array Salary by .10 (10 percent), and save the result into array Tax. Print/display the content of both arrays. Hint: the content of array Tax is 10% percent of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT