-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap_Sort
More file actions
47 lines (44 loc) · 813 Bytes
/
Heap_Sort
File metadata and controls
47 lines (44 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
#include <bits/stdc++.h>
using namespace std;
int A[1001];
void Max_Heapify(int n , int i)
{
int largest = i;
int l = 2 * i + 1; /// left
int r = 2 * i + 2; /// right
if( l < n && A[l] > A[i])
{
largest = l;
}
else
largest = i;
if(r < n && A[r] > A[largest])
{
largest = r;
}
if(largest != i)
{
swap(A[i] , A[largest]);
Max_Heapify(n , largest);
}
}
void heap_sort(int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
Max_Heapify(n , i);///heapify(i);
for(int i = n - 1; i >= 0; i--)
{
swap(A[0] , A[i]);
Max_Heapify(i,0);
}
}
int main()
{
int n;
cin >> n;
for(int i = 0; i < n ;i++)
cin >> A[i];
heap_sort(n);
for(int i = 0; i < n; i++)
cout<<A[i] << " ";
}