forked from Anubhav-1020/Hacktoberfest-Beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapsort.c
66 lines (52 loc) · 714 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<stdio.h>
void insert(int a[],int n)
{
int i=n;
int temp=a[i];
while(i>1 && temp >a[i/2])
{
a[i]=a[i/2];
i=i/2;
}
a[i]=temp;
}
int delete(int a[],int n)
{
int temp=a[1];
int i,j;
a[1]=a[n];
a[n]=temp;
i=1;j=i*2;
while(j < n-1)
{
if(a[j+1] > a[j])
{
j=j+1;
}
if(a[i] <a[j])
{
int p=a[i];
a[i]=a[j];
a[j]=p;
i=j;
j=2*j;
}
else
break;
}
}
int main()
{
int a[]={0,10,20,30,40,50};
int i;
for(i=2;i<=5;i++)
{
insert(a,i);
}
for(i=5;i>1;i--)
{
delete(a,i);
}
for(i=1;i<=5;i++)
printf(" %d ",a[i]);
}