-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapSort.c
More file actions
65 lines (46 loc) · 1.03 KB
/
heapSort.c
File metadata and controls
65 lines (46 loc) · 1.03 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
#include<stdio.h>
#define LENGTH 10
void swap(int *data, int root, int max){
int temp = data[max];
data[max] = data[root];
data[root] = temp;
return;
}
void heapify(int *data, int root, int length){
int rightchild = root*2+2;
int leftchild = root*2+1;
int maxnode = 0;
if(leftchild<length && data[leftchild] > data[root])
maxnode = leftchild;
else
maxnode = root;
if(rightchild<length && data[rightchild] > data[maxnode])
maxnode = rightchild;
if(maxnode != root){
swap(data, root, maxnode);
heapify(data, maxnode, length);
}
}
void heapSort(int *data){
int i;
//將數列轉換成Max Heap
for(i=(LENGTH/2 -1) ; i>=0 ; i--)
heapify(data, i, LENGTH);
for(i=0 ; i<LENGTH ; i++)
printf("value is %d\n", data[i]);
//排序
for(i = LENGTH-1 ; i>0 ; i--){
swap(data, 0, i);
heapify(data, 0, i);
}
printf("\nAfter Sorted : \n");
for(i=0 ; i<LENGTH ; i++)
printf("value is %d\n", data[i]);
return;
}
int main(void)
{
int data[10] = {92,38,59,57,14,52,19,69,23,84};
heapSort(data);
return 0;
}