-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec9_3.cpp
More file actions
47 lines (38 loc) · 813 Bytes
/
Copy pathlec9_3.cpp
File metadata and controls
47 lines (38 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// step down method ; array as input
#include<iostream>
using namespace std;
void Heapify(int arr[] , int index , int n)
{
int largest = index;
int left = 2*index + 1;
int right = 2*index + 2;
if(left < n && arr[left] > arr[largest])
largest = left;
if(right <n && arr[right] > arr[largest])
largest = right;
if(largest != index)
{
swap(arr[largest] , arr[index]);
Heapify(arr , largest , n);
}
}
void BuildHeap(int arr[],int n)
{
for( int i = n/2-1; i>=0 ; i--)
{
Heapify(arr , i , n);
}
}
void printHeap(int arr[] , int n)
{
for(int i = 0 ; i<n ; i++)
{
cout<<arr[i]<<" ";
}
}
int main()
{
int arr[] = { 10 , 3 , 8 , 9 , 5 , 13 , 18 , 14 , 11 , 70};
BuildHeap(arr , 10);
printHeap(arr , 10);
}