Illustrate the HeapSort on the following array. [ 7, 2, 4, 6, 3, 1, 5 ] Show the array and the heap after each call to ReHeap.
C++ program for implementation of Heap Sort :
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr[l] >
arr[largest])
largest = l;
// If right child is larger than largest so
far
if (r < n && arr[r] >
arr[largest])
largest = r;
// If largest is not root
if (largest != i) {
swap(arr[i], arr[largest]);
// Recursively heapify the
affected sub-tree
heapify(arr, n, largest);
}
}
// main function to do heap sort
void heapSort(int arr[], int n)
{
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// One by one extract an element from heap
for (int i = n - 1; i >= 0; i--) {
// Move current root to end
swap(arr[0], arr[i]);
// call max heapify on the
reduced heap
heapify(arr, i, 0);
}
}
/* A utility function to print array of size n */
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
cout << arr[i] << "
";
cout << "\n";
}
void print(const int &i) {
std::cout << i << ' ';
}
int main()
{
int arr[] = { 7, 2, 4, 6, 3, 1, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Given Array List Before HeapSort: \n";
std::for_each(std::begin(arr), std::end(arr), print);
cout<< "\n";
heapSort(arr, n);
cout << "Sorted array After HeapSort: \n";
printArray(arr, n);
return 0;
}
Output:
Given Array List Before HeapSort: 7 2 4 6 3 1 5 Sorted array After HeapSort: 1 2 3 4 5 6 7
==================================================================
Code Snap Pictures:
Output:
Please give Thumbsup....
Illustrate the HeapSort on the following array. [ 7, 2, 4, 6, 3, 1, 5 ]...
Use Figure 6.4 as a model, illustrate the operation of HEAPSORT on the array A = <4, 10, 7, 25, 8, 3>. Show all intermediate steps how the heap is transformed. 6.4/ 91|11|016 | 8|4|||7|V (9) 09
in c++ please. thank you! Page 4 of 4 5. Heap and heapsort: answer the following three questions. a) (1 pt) What is the definition of a max heap? | 0 b) (2 pts) When we insert an element, 5, to the following max heap, what would be the resulting max heap? Give the detailed procedure. (14) (10) c) (2 pts) Based on b), when we remove the root element in the max heap, what would be the resulting max...
Consider the following max-heap stored as an array: <7, 6, 4, 2, 5, 1, 3>. Draw this max-heap as an (undirected) binary tree and give both adjacency-list representation and adjacency-matrix representation of the binary tree
2. If you are using HeapSort to put elements in ascending order, what kind of heap should you use? Why? Conduct your HeapSort uisng the sequence given in task 1 (show the array after each round of sorting) (6 points)
Question 3. a. Draw the binary min heap represented by the following array: (5 points) 1 2 4 6 7 Value 4 9 12 29 17 14 16 b. Show the result of calling deleteMin twice on the heap you drew in part (a). Show the heap after each deleteMin, and circle the final heap. (5 points) c. Starting with the heap you ended up with in part (b), insert values 11 & 2 in that order. Draw the heap...
Subject: Algorithm need this urgent please. 2.1 Searching and Sorting- 5 points each 1. Run Heapsort on the following array: A 17, 3, 9, 4, 2,5, 6, 1,8) 2. Run merge sort on the same array 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Explain what modifications we can make to quick sort to make it run faster, and why this helps. 2.1 Searching and Sorting-...
Illustrate the operation of randomized quicksort on the array: A = (19, 2, 11, 14, 7, 17, 4, 3, 5, 15) by showing the values in A after each call to partition. Assume that the randomly chosen pivots were, in order, 〈19, 14, 2, 5, 4, 7, 15〉.
Please ignore red marks. Thanks 6. (8 pts) Illustrate the algorithmic operations on the maximum binary heap data sti 'perations on the maximum binary heap data structure as directed. BUILD-MAX-HEAP(A) MAX-HEAPIFY (A. i) 1 A heap-size = A.length 11 = LEFT() 2 for i = A.length/2) downto 1 2 r = RIGHT() 3 MAX-HEAPIFY (A,i) 3 if / S 4.heap-size and All > A[i] HEAP-EXTRACT-MAX (A) 4 largest = 1 5 else largest = 1 1 if A.heap-size <1 6...
[5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9, 4, 3). Assuming we call the following function using statement selection sort (int array, 7); Please fill the table to show the changes of the array int_array after each iteration. The first column is filled. void selection_sort (int list[], int list_size) for (int i = 0; i < list size - 1; 1++) int current min = list[1]; int current_min_index-i for (int j -...
1. Consider an array of size eight with the numbers in the following order 40, 20, 80, 60, 30, 10, 70, 50. (a) What is the array after heap creation? Make sure to form the heap bottom up as done in class. How many comparisons does the algorithm use? (b) Show the array after each element sifts down during the remainder of heapsort, and state how many comparisons each sift takes. What is the total number of comparisons for the...