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
#include <stdio.h>
 
int number = 9;
int heap[9= { 7,6,5,8,3,5,9,1,6 };
 
int main() {
    //힙을 구성
    for (int i = 1; i < number; i++) {
        int c = i;
        do {
            int root = (c - 1/ 2;
            if (heap[root] < heap[c]) {
                int temp = heap[root];
                heap[root] = heap[c];
                heap[c] = temp;
            }
            c = root;
        } while (c != 0);
    }
    //크기를 줄여가며 반복적으로 힙을 구성
    for (int i = number - 1; i >= 0; i--) {
        int temp = heap[0];
        heap[0= heap[i];
        heap[i] = temp;
        int root = 0;
        int c = 1;
        do {
            c = 2 * root + 1;
            //자식 중에 더 큰 값을 찾기
            if (c < i - 1 && heap[c] < heap[c + 1]) {
                c++;
            }
            //루트보다 자식이 크다면 교환
            if (c < i && heap[root] < heap[c]) {
                temp = heap[root];
                heap[root] = heap[c];
                heap[c] = temp;
            }
            root = c;
        } while (c < i);
    }
    //결과 출력
    for (int i = 0; i < number; i++) {
        printf("%d ", heap[i]);
    }
}
cs

 

'C,C++ > Algorithm' 카테고리의 다른 글

Algorithm, c++, Sort  (0) 2019.09.23
Algorithm, c++, BFS와 DFS 구현  (0) 2019.09.23

+ Recent posts