Title: Data Structure
1Data Structure
- Week 6 Tree (Heap / Sort)
- Homepage www.cs.hongik.ac.kr/rhanha
- E-mail jhbang_at_cs.hongik.ac.kr
2??
- Heap? ??? Sort ?? ??
- Heap sort ??
- ?? ??
- ?? ??
- HW3 ?????? ??
31. Heap sort
? ??(Heap sort) ??? ????? ?? ??? ???? ????
??. ? ??? ????? ?? ??? ???? ?? ?? ??? ??? ?? ? ??
??? ??? ??? ??? ? ?? ? ?? ??? ?? ??. ? ?? ???
????? ?? ?? ?? ???? ??? ??? ??? ????? ?? ?? ?????
?? ???? ????
41. Heap sort (cont.)
? ?? 1. ?? ?? ?? 2. ??? ??? ?? ?? ???? ??? ??
?? - n?? ? ???? ?? ? ?? ?? ?????? ? ??? ???
??(n)? ???? ??? ??(?? ?)? ????? ?? -
n-1?? ?? ?????, ?? ???? ? ??? ????? ??.
???? ?? ???? ?? ???? ??? ?? ?? - n-1?? ??
??? ??? ??? ??(n-1)? ???? ?? ??? ????? ??
??? ???? n, n-1, n-2, ? ?? ???? ???? ?????
? ? ?? ??? ?? ????? ??? ??
52. Heap sort????
62. Heap sort????(cont.)
include ltstdio.hgt void swap(int a, int b) /
?? ?? / int w wa ab
bw
72. Heap sort????(cont.)
void main(void) static int heap1000
int n, i, s, p, m n1
while(scanf("d", heapn)!EOF) / ?? ? ?? /
sn ps/2 while(sgt2
heappgtheaps) swap(heapp,
heaps) sn ps/2 n
82. Heap sort????(cont.)
0 1 2 3 4 5 6 7 8 9
sn ps/2 n2 p1 n3 p1 n4
p2 n5 p2 n6 p3 n7
p3 n8 p4 n9 p4
92. Heap sort????(cont.)
n-- / ??? ?/ mn / n? ??
/ while(ngt1) swap(heap1, heapn)
n-- / ?? ??? ?? ?? / p1 s2p
while(sltn) if(sltn heaps1ltheaps)
s if(heappltheaps)
break swap(heapp, heaps)
ps s2p for(i1iltmi) printf(d,
heapi)
103. HW2
- Heap sort? ??
- ?????? ???? ???
- ?? ?
- Input 8 2 6 4 5 Z
- Output 2 4 5 6 8
- ?? ??
- ??? Heap? ???? ??
- Due day 5? 2? 24???