7. heap sort - kocwcontents.kocw.net/kocw/document/2015/yeungnam/... · 2016. 9. 9. · 7. heap...

12
7. 정렬 (Page 40) 7. Heap Sort 특징 Require only a fixed amount of additional storage Worst/Average case computing time = O(n log n) Space complexity = O(1) 기본 개념 MAX heapn 번의 insert & delete : O(n log n) More fast algorithm 노드 수가 2 이상인 subtree대해 heap 구성 MAX heap에서 n 번의 delete Program 7.13 ~ 7.14

Upload: others

Post on 04-Sep-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 7. Heap Sort - KOCWcontents.kocw.net/KOCW/document/2015/yeungnam/... · 2016. 9. 9. · 7. Heap Sort . . 특징. . Require only a fixed amount of additional storage . . Worst/Average

7장. 정렬 (Page 40)

7. Heap Sort

특징 Require only a fixed amount of additional storage Worst/Average case computing time = O(n log n) Space complexity = O(1)

기본 개념 MAX heap에 n 번의 insert & delete : O(n log n) More fast algorithm

노드 수가 2 이상인 subtree에 대해 heap 구성 MAX heap에서 n 번의 delete Program 7.13 ~ 7.14

Page 2: 7. Heap Sort - KOCWcontents.kocw.net/KOCW/document/2015/yeungnam/... · 2016. 9. 9. · 7. Heap Sort . . 특징. . Require only a fixed amount of additional storage . . Worst/Average

7장. 정렬 (Page 41)

Program 7.13: Adjusting a Max Heap void adjust (int list[ ], int root, int n) { // root를 제외한 트리의 나머지 부분은 max heap으로 이미 조직 // list[root]의 위치를 조정하여 max heap을 재구성 int rootkey = list[root], child = 2 * root; // left child while (child <= n) { if ((child < n) && (list[child] < list[child+1])) child++; // 왼쪽 또는 오른쪽 중에서 큰 값을 child로 설정. if (rootkey > list[child]) break; else { list[child / 2] = list[child]; // child가 클 경우 부모 자리로. child *= 2; } } list[child / 2] = rootkey; }

Page 3: 7. Heap Sort - KOCWcontents.kocw.net/KOCW/document/2015/yeungnam/... · 2016. 9. 9. · 7. Heap Sort . . 특징. . Require only a fixed amount of additional storage . . Worst/Average

7장. 정렬 (Page 42)

Program 7.14: Heap Sort void heapsort (int list[ ], int n) { // list[1]부터 list[n]까지를 오름차순으로 정렬 int i, j; int temp;

for (i = n/2; i > 0; i--) // 초기 max heap을 구성 adjust (list, i, n); // n/2은 child를 갖는 첫번째 노드 for (i = n-1; i > 0; i--) { SWAP (list[1], list[i+1], temp); // root를 맨 뒤의 원소와 교환 adjust (list, 1, i); // 원소를 하나 적게 가진 heap을 재구성 } }

Page 4: 7. Heap Sort - KOCWcontents.kocw.net/KOCW/document/2015/yeungnam/... · 2016. 9. 9. · 7. Heap Sort . . 특징. . Require only a fixed amount of additional storage . . Worst/Average

7장. 정렬 (Page 43)

Heap Sort의 동작 과정(1)

5

26

77

1 61

[1]

[2] [3]

[4] [5] [6]

11 59 [7]

15 48 [8] [9] [10] 19

초기 트리

61

77

59

48 19

[1]

[2] [3]

[4] [5] [6]

11 26 [7]

15 1 [8] [9] [10] 5

첫번째 for loop을 실행하고 난 뒤의 max heap

Page 5: 7. Heap Sort - KOCWcontents.kocw.net/KOCW/document/2015/yeungnam/... · 2016. 9. 9. · 7. Heap Sort . . 특징. . Require only a fixed amount of additional storage . . Worst/Average

7장. 정렬 (Page 44)

Heap Sort의 동작 과정(2)

48

61

59

15 19

[1]

[2] [3]

[4] [5] [6]

11 26 [7]

5 1 [8] [9] [10] 77

(a)

48

59

26

15 19

[1]

[2] [3]

[4] [5] [6]

11 1 [7]

5 61 [8] [9] [10] 77

(b)

19

48

26

15 5

[1]

[2] [3]

[4] [5] [6]

11 1 [7]

59 61 [8] [9] [10] 77

(c)

19

26

11

15 5

[1]

[2] [3]

[4] [5] [6]

1 48 [7]

59 61 [8] [9] [10] 77

(d)

Page 6: 7. Heap Sort - KOCWcontents.kocw.net/KOCW/document/2015/yeungnam/... · 2016. 9. 9. · 7. Heap Sort . . 특징. . Require only a fixed amount of additional storage . . Worst/Average

7장. 정렬 (Page 45)

Heap Sort의 복잡성 가정 : 2k-1 ≤ n < 2k, level i 의 노드 수 = 2i-1

First for loop

Second for loop : O(n log n)

)(22

2)(21

1

1

1

11

1

1 nOniniikk

ii

k

i

ikk

i

i =<≤=− ∑∑∑−

=

=

−−−

=

Page 7: 7. Heap Sort - KOCWcontents.kocw.net/KOCW/document/2015/yeungnam/... · 2016. 9. 9. · 7. Heap Sort . . 특징. . Require only a fixed amount of additional storage . . Worst/Average

연습 문제 배열에 저장된 10개의 데이터들을 히프 정렬(heap sort) 알고리즘을 이용하여 오름차순으로 정렬하고자 한다. 초기 heap의 내용과, heap에서 첫 번째 삭제 단계가 이루어지고 난 다음의 내용을 각각 작성하라.

7장. 정렬 (Page 46)

[1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

31 5 88 3 61 11 59 24 45 19

[1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

[1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

초기 데이터

초기 Heap

첫 번째 단계

Page 8: 7. Heap Sort - KOCWcontents.kocw.net/KOCW/document/2015/yeungnam/... · 2016. 9. 9. · 7. Heap Sort . . 특징. . Require only a fixed amount of additional storage . . Worst/Average

Heap Sort의 성능(1)

데이터가 정렬되지 않은 경우

7장. 정렬 (Page 47)

Page 9: 7. Heap Sort - KOCWcontents.kocw.net/KOCW/document/2015/yeungnam/... · 2016. 9. 9. · 7. Heap Sort . . 특징. . Require only a fixed amount of additional storage . . Worst/Average

Heap Sort의 성능(2)

데이터가 정렬된 경우

7장. 정렬 (Page 48)

Page 10: 7. Heap Sort - KOCWcontents.kocw.net/KOCW/document/2015/yeungnam/... · 2016. 9. 9. · 7. Heap Sort . . 특징. . Require only a fixed amount of additional storage . . Worst/Average

Heap Sort의 성능(3)

데이터의 크기가 큰 경우

7장. 정렬 (Page 49)

Page 11: 7. Heap Sort - KOCWcontents.kocw.net/KOCW/document/2015/yeungnam/... · 2016. 9. 9. · 7. Heap Sort . . 특징. . Require only a fixed amount of additional storage . . Worst/Average

Sorting Algorithm의 비교

7장. 정렬 (Page 50)

Name 최선 평균 최악 Memory Stable 기타

Selection sort n2 n2 n2 1 No •IS에 비해 Write 연산의 수

가 적다(for Flash Memory)

Insertion sort n n2 n2 1 Yes •SS에 비해 비교 연산의 수

가 적다

Quick sort n log n n log n n2 log n,

or n No •가장 빠른 내부 정렬 알고리즘으로 알려져 있음

Merge sort n log n n log n n log n n Yes

• 외부 정렬에 적합 • 연결 리스트 정렬 지원 • 병렬 처리에 적합

Heap sort n log n n log n n log n 1 No

• Quick sort보다는 늦지만, • 최악의 경우도 n log n • Memory도 적게 사용

Page 12: 7. Heap Sort - KOCWcontents.kocw.net/KOCW/document/2015/yeungnam/... · 2016. 9. 9. · 7. Heap Sort . . 특징. . Require only a fixed amount of additional storage . . Worst/Average

데이터 수에 따른 성능 평가 데이터 크기 = 4, 정렬 여부 = 아니오, 단위 = 초

7장. 정렬 (Page 51)

데이터 수 Quick Merge Heap

10000 0.00 0.00 0.00 210000 0.02 0.03 0.04 410000 0.03 0.04 0.04 610000 0.05 0.06 0.07 810000 0.06 0.08 0.10

1010000 0.08 0.10 0.12 1110000 0.09 0.12 0.14 1310000 0.11 0.14 0.16 1510000 0.12 0.16 0.19 1710000 0.14 0.18 0.22 1910000 0.15 0.21 0.25