0% found this document useful (0 votes)
8 views

Lecture-074 2 Build Min Heap - CPP

The document discusses heap sorting and building a min heap. It includes code to heapify a tree and build a min heap from an array. The code demonstrates heapifying an array, printing it before and after building the min heap.

Uploaded by

maragonirohansai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture-074 2 Build Min Heap - CPP

The document discusses heap sorting and building a min heap. It includes code to heapify a tree and build a min heap from an array. The code demonstrates heapifying an array, printing it before and after building the min heap.

Uploaded by

maragonirohansai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1 #include<bits/stdc++.

h>usingnamespace/*
2 0 based indexing
3 left - 2*i + 1;
4 right - 2*i + 2;
5 */voidheapify(vector<intintintintint21int22ifififswapheapifyvector<intbuildMinHeap
(vector<intintsizeforint2-10heapifyreturnintmain()int903020120506040150// Heapifing
array"Before building Min Heap : "forint0size" "buildMinHeap"After building Min Heap : "
forint0size" "return0</span>
6 std;
7 >& arr, n, i)</span></span>{
8 smallest = i;
9 left = *i + ;
10 right = *i + ;
11 (left < n && arr[smallest] > arr[left]) {
12 smallest = left;
13 }
14 (right < n && arr[smallest] > arr[right]) {
15 smallest = right;
16 }
17 (smallest != i) {
18 (arr[i], arr[smallest]);
19 (arr, n, smallest);
20 }
21 }
22 > >& arr)</span></span>{
23 n = arr.();
24 ( i=(n/); i>=; i--) {
25 (arr, n, i);
26 }
27 arr;
28 }</span>{
29 vector<> arr = {, , , , , , , };
30 cout << ;
31 ( i=; i<arr.(); i++) {
32 cout << arr[i] << ;
33 }
34 cout << endl;
35 arr = (arr);
36 cout << ;
37 ( i=; i<arr.(); i++) {
38 cout << arr[i] << ;
39 }
40 cout << endl;
41 ;
42 }

You might also like