본문 바로가기

알고리즘 문제풀이

[Heap]Heap Sort

반응형
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
 
#define NUM_DATA    (8)
#define MAX_DATA    (NUM_DATA)
#define MAX_HEAP    (MAX_DATA+1)
#define LAST_NODE    (MAX_DATA)
 
int heap[MAX_HEAP];
int lastnode = 0;
 
int Heap_Push_Minheap(int *heap, int sizeint *lastnode, int d)
{
    int n,p,temp;
 
    if(*lastnode == sizereturn 0;
    
    heap[++(*lastnode)] = d;
 
    n = *lastnode; p = n/2;
 
    while(p>0)
    {
        if(heap[n] < heap[p])
        {
            temp = heap[n];
            heap[n] = heap[p];
            heap[p] = temp;
            n = p; p = n / 2;
        }
        else break;
    }
 
    return 1;
}
 
int Heap_Pop_Minheap(int *heap, int *lastnode, int *d)
{
    int n,c,lc,rc,temp;
 
    if(*lastnode == 0return 0;
 
    *= heap[1];
    heap[1= heap[(*lastnode)--];
 
    n = 1; lc = 2; rc = 3;
    while(lc<=*lastnode)
    {
        if(*lastnode == lc) c = lc;
        else c = (heap[lc]<heap[rc])?(lc):(rc);
    
        if(heap[c]<heap[n])
        {
            temp = heap[c];
            heap[c] = heap[n];
            heap[n] = temp;
            n = c; lc = n*2; rc = lc+1;
        }
        else break;
    }
 
    return 1;
}
 
int Heap_Push_Maxheap(int *heap, int sizeint *lastnode, int d)
{
    int n,p,temp;
 
    if(*lastnode == sizereturn 0;
    
    heap[++(*lastnode)] = d;
 
    n = *lastnode; p = n/2;
 
    while(p>0)
    {
        if(heap[n] > heap[p])
        {
            temp = heap[n];
            heap[n] = heap[p];
            heap[p] = temp;
            n = p; p = n / 2;
        }
        else break;
    }
 
    return 1;
}
 
int Heap_Pop_Maxheap(int *heap, int *lastnode, int *d)
{
    int n,c,lc,rc,temp;
 
    if(*lastnode == 0return 0;
 
    *= heap[1];
    heap[1= heap[(*lastnode)--];
 
    n = 1; lc = 2; rc = 3;
    while(lc<=*lastnode)
    {
        if(*lastnode == lc) c = lc;
        else c = (heap[lc]>heap[rc])?(lc):(rc);
    
        if(heap[c]>heap[n])
        {
            temp = heap[c];
            heap[c] = heap[n];
            heap[n] = temp;
            n = c; lc = n*2; rc = lc+1;
        }
        else break;
    }
 
    return 1;
}
 
void main(void)
{
 
}
 
// 작성한 코드를 아래에 넣으시오.
 
typedef int (*HEAP_PUSH)(int*,int,int*,int);
typedef int (*HEAP_POP)(int*,int*,int*);
 
void Heap_Sort(int *data,int cnt,int order)
{
    int i,ret,lastnode=0;
    HEAP_PUSH Heap_Push[2= {Heap_Push_Maxheap,Heap_Push_Minheap};
    HEAP_POP Heap_Pop[2= {Heap_Pop_Maxheap,Heap_Pop_Minheap};
 
    for(i=0;i<cnt;i++)
    {
        Heap_Push[order](data-1,MAX_DATA,&lastnode,data[i]);
    }
 
    for(i=cnt-1;i>=0;i--)
    {
        Heap_Pop[order](data-1,&lastnode,&ret);
        data[i] = ret;
    }
}
cs

" target="_blank" rel="noopener" data-mce-href="http://

 

">http://

 

 

반응형

'알고리즘 문제풀이' 카테고리의 다른 글

[Hash] Chainning 방식  (0) 2020.10.07
[Hash]Open Address 방식  (0) 2020.10.07
[Heap]MaxHeap구성  (0) 2020.10.07
[HEAP] minheap 구성  (0) 2020.10.07
SegmentTree[구간의 최대값 구하기]  (0) 2020.10.07