-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx.c
More file actions
93 lines (80 loc) · 1.41 KB
/
x.c
File metadata and controls
93 lines (80 loc) · 1.41 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<stdio.h>
int a[10];
void print(int a[],int n)
{
int i;
for(i=1;i<=n;i++)
{
printf("%d",a[i]);
}
}
void swap(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void maxheap(int a[],int pos,int si)
{
int l,r,x;
l=2*pos;
r=2*pos+1;
printf("give %d,left=%d,size=%d",pos,l,si);
printf("%d,%d",a[pos],a[l]);
if((l<=si)&&(a[l]>a[pos]))
{
x=l;
}
else
{
x=pos;
}
if((r<=si)&&a[r]>a[x])
{
x=r;
}
printf("max at%d\n",x);
printf("pos %d x=%d",pos,x);
if(x!=pos)
{
swap(a,pos,x);
maxheap(a,x,si);
}
}
void buildheap(int a[],int n)
{
int i;
for(i=(n/2);i>=1;i--)
{
maxheap(a,i,n);
}
}
void heapsort(int a[],int n)
{
int i;
int one=1;
buildheap(a,n);
printf("after buildheap\n");
print(a,n);
for(i=n;i>=2;i--)
{
swap(a,i,one);
buildheap(a,i-1);
}
}
int main()
{
int size,i;
printf("give the size\n");
scanf("%d",&size);
printf("enter elements\n");
for(i=1;i<=size;i++)
{
scanf("%d",&a[i]);
}
//print(a,size);
heapsort(a,size);
print(a,size);
return 0;
}