-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheapsort.c
More file actions
81 lines (57 loc) · 959 Bytes
/
heapsort.c
File metadata and controls
81 lines (57 loc) · 959 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
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
/*
* Name :sharad Dixit
* Institute :IIIT -A
*/
#include <stdio.h>
void heapify( int arr[],int bound)
{
int root;
int lchild;
int mchild;
int rchild;
int i;
int temp;
for ( i = bound/2;i >=1;i--){
// root=a[i];
lchild=2*i;
rchild=2*i+1;
if ( lchild <= bound && rchild<=bound) {
if(arr[rchild] >= arr[lchild])
mchild = rchild;
else
mchild = lchild;
}
else{
mchild= lchild;
}
//printf("value of arr[mchild] %d",arr[mchild]);
//printf("value of arr[i] %d",arr[i]);
if (arr[i] < arr[mchild])
{
temp = arr[i];
arr[i] = arr[mchild];
arr[mchild] = temp;
}
}
temp = arr[1];
arr[1] = arr[bound];
arr[bound] = temp;
return;
}
int main()
{
int a [100];
int i=1,j;
int number;
scanf("%d",&number);
while ( i <= number){
scanf("%d",&a[i]);
i++;
}
for ( j = 0; j < number; j++) {
heapify( a, number-j );
}
for ( j = 1; j <=number; j++) {
printf("---->%d\n",a[j]);
}
}