-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminheapsort.cpp
67 lines (58 loc) · 1.14 KB
/
minheapsort.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream>
using namespace std;
void heapsort(int a[], int);
void buildHeap(int a[], int, int);
void minheapify(int a[], int, int);
int main()
{
int i,n,t;
cin>>t;
while(t--)
{
cin>>n;
int a[n];
for(i=0;i<n;i++)
cin>>a[i];
heapsort(a,n);
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}
return 0;
}
void heapsort(int a[], int n)
{
int heapsize = n-1;
buildHeap(a, n, heapsize);
for(int i=n-1;i>=1;i--)
{
swap(a[i],a[0]);
heapsize -=1;
minheapify(a,0,heapsize);
}
}
void buildHeap(int a[],int n, int heapsize)
{
for(int i = (n/2)-1;i>=0;i--)
{
minheapify(a,i,heapsize);
}
}
void minheapify(int a[],int i, int heapsize)
{
int l,r,smallest=i;
l = 2*i+1;
r = 2*i+2;
if(l<=heapsize && a[l]<a[i])
smallest = l;
else
smallest = i;
if(r<=heapsize && a[smallest]>a[r])
smallest = r;
if(smallest != i)
{
swap(a[i],a[smallest]);
minheapify(a,smallest, heapsize);
}
}