-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathheapsort.c
57 lines (47 loc) · 1.11 KB
/
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
/***********************************************************
heapsort.c -- ヒープソート
***********************************************************/
typedef int keytype;
void heapsort(int n, keytype a[])
{
int i, j, k;
keytype x;
for (k = n / 2; k >= 1; k--) {
i = k; x = a[i];
while ((j = 2 * i) <= n) {
if (j < n && a[j] < a[j + 1]) j++;
if (x >= a[j]) break;
a[i] = a[j]; i = j;
}
a[i] = x;
}
while (n > 1) {
x = a[n]; a[n] = a[1]; n--;
i = 1;
while ((j = 2 * i) <= n) {
if (j < n && a[j] < a[j + 1]) j++;
if (x >= a[j]) break;
a[i] = a[j]; i = j;
}
a[i] = x;
}
}
#include <stdio.h>
#include <stdlib.h>
#define N 20
int a[N + 1];
int main(void)
{
int i;
printf("Before:");
for (i = 1; i <= N; i++) {
a[i] = rand() / (RAND_MAX / 100 + 1);
printf(" %2d", a[i]);
}
printf("\n");
heapsort(N, a);
printf("After: ");
for (i = 1; i <= N; i++) printf(" %2d", a[i]);
printf("\n");
return 0;
}