forked from amanss00/ForNewbies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heapsort.c
50 lines (46 loc) · 845 Bytes
/
heapsort.c
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
#include <stdio.h>
void swap(int a[], int i,int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
void maxheapify(int a[],int n,int k){
int l,r,max;
l=2*k;
r=l+1;
if(l<=n && a[l]>a[k])
max=l;
else
max=k;
if(r<=n && a[r]>a[max])
max=r;
if(max!=k){
swap(a,k,max);
maxheapify(a,n,max);
}
}
void build_maxheap(int a[],int n){
for(int i=n/2;i>=1;i--)
maxheapify(a,n,i);
}
void heapsort(int a[],int n){
build_maxheap(a,n);
for(int i=1;i<=n-1;i++){
swap(a,1,n-i+1);
maxheapify(a,n-i,1);
}
}
int main() {
int i,n;
printf("Enter the Size of the Array :\n");
scanf("%d",&n);
int a[n];
printf("Enter the Elements of the Array :\n");
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
heapsort(a,n);
printf("Sorted array:\n");
for (i = 1; i <=n; ++i)
printf("%d\t", a[i]);
return 0;
}