1 / 21

알고리즘 CHAPTER 3.2 고급 정렬 알고리즘

알고리즘 CHAPTER 3.2 고급 정렬 알고리즘. 천주희 wngml1205@kunsan.ac.kr 군산대학교 통계컴퓨터과학과 정보과학기술 연구실 2012.09.14. 목차. 병합 정렬 퀵 정렬 힙 정렬. 병합 정렬 (1/7). 정렬할 배열이 주어짐. 배열을 반반으로 나눔. 각각 독립적으로 정렬. 병합 ( 정렬완료 ). 병합 정렬 (2/7). mergeSort (A[ ], p, r) ▷ A[p ... r] 을 정렬한다 { if (p < r) then {

yoshe
Télécharger la présentation

알고리즘 CHAPTER 3.2 고급 정렬 알고리즘

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 알고리즘CHAPTER 3.2 고급 정렬 알고리즘 천주희 wngml1205@kunsan.ac.kr • 군산대학교 통계컴퓨터과학과 정보과학기술 연구실 2012.09.14

  2. 목차 병합 정렬 퀵 정렬 힙 정렬 IST (Information Sciences & Technology) Laboratory

  3. 병합 정렬 (1/7) 정렬할 배열이 주어짐 배열을 반반으로 나눔 각각 독립적으로 정렬 병합(정렬완료) IST (Information Sciences & Technology) Laboratory

  4. 병합 정렬 (2/7) mergeSort(A[ ], p, r) ▷ A[p ... r]을 정렬한다 { if (p < r) then {                 q ← (p+r)/2;    -----------------------  ①   ▷ p, q의 중간 지점 계산 mergeSort(A, p, q);  ----------------  ②   ▷ 전반부 정렬 mergeSort(A, q+1, r); --------------  ③   ▷ 후반부 정렬 merge(A, p, q, r);  ------------------  ④   ▷ 병합 } } merge(A[ ], p, q, r) { 정렬되어 있는 두 배열 A[p ... q]와 A[q+1 ... r]을 합하여 정렬된 하나의 배열 A[p ... r]을 만든다. } 병합 정렬 알고리즘 IST (Information Sciences & Technology) Laboratory

  5. 병합 정렬(3/7) p q r j i t j i t j i t mergy() 함수 IST (Information Sciences & Technology) Laboratory

  6. 병합 정렬(4/7) j i t i j t j i t mergy() 함수 IST (Information Sciences & Technology) Laboratory

  7. 병합 정렬(5/7) i j j i t t i j i j t t mergy() 함수 IST (Information Sciences & Technology) Laboratory

  8. 병합 정렬 (6/7) p q r i j • merge (A[ ], p, q, r) ▷ A[p ... q]와 A[q+1 ... r]를 병합하여 A[p … r]을 정렬된 상태로 만든다. ▷ A[p ... q]와 A[q+1 ... r]는 이미 정렬되어 있다. { i ← p; j ← q+1; t ← 1; while (i≤ q and j ≤ r){ if(A[i] ≤ A[j]) thentmp[t++] ← A[i++]; elsetmp[t++] ← A[j++]; } while(i ≤ q) tmp[t++] ← A[i++]; while(j ≤ r) tmp[t++] ← A[j++]; i ← p; t ← 1; while(i ≤ r) A[i++] ← tmp[t++]; } t mergy() 함수 IST (Information Sciences & Technology) Laboratory

  9. 병합 정렬(7/7) T(n) = 2T(n/2) + cn =2(2T(n/22) + cn/2) + cn= 22T(n/22) + 2cn = 22(2T(n/23) + cn/22) + 2cn= 23T(n/23) + 3cn … ≤2kT(n/2k) + kcn = n T(1)+ kcn=an+cnlogn = Θ(nlogn) 수행 시간 IST (Information Sciences & Technology) Laboratory

  10. 퀵 정렬 (1/6) 정렬할 배열이 주어짐 (기준: 맨 뒤 수) 기준보다 작은 수는 기준의 왼쪽에 나머지는 기준의 오른쪽에 오도록 재배치 (a) 기준(31) 왼쪽과 오른쪽을 각각 독립적으로 정렬 (정렬완료) (b) IST (Information Sciences & Technology) Laboratory

  11. 퀵 정렬 (2/6) quickSort(A[], p, r) ▷ A[p ... r]을 정렬한다 { if(p < r) then{ q = partition(A, p, r);  ▷ 분할 quickSort(A, p, q-1);   ▷ 왼쪽 부분배열 정렬 quickSort(A, q+1, r);   ▷ 오른쪽 부분배열 정렬 } } partition(A[], p, r) { 배열 A[p ... r]의 원소들을 A[r]을 기준으로 양쪽으로 재배치하고 A[r]이 자리한 위치를 return한다; }   퀵 정렬 알고리즘 IST (Information Sciences & Technology) Laboratory

  12. 퀵 정렬 (3/6) i j j i j i (a) j i (b) i j partition() 함수 IST (Information Sciences & Technology) Laboratory

  13. 퀵 정렬 (4/6) (c) j i i j j i i j (d) i (e) i IST (Information Sciences & Technology) Laboratory

  14. 퀵 정렬 (5/6) r p i j partition(A[], p, r) { x ← A[r]; i ← p – 1; for j ← p to r – 1 if(A[j] ≤ x) then A[++i] ↔ A[j]; A[i+1] ↔ A[r]; return i+1; } partition() 함수 IST (Information Sciences & Technology) Laboratory

  15. 퀵 정렬 (6/6) • 수행 시간 • 평균적인 경우 • Θ(nlogn) • 최악의 경우 • 한쪽은 하나도 없고 다른 쪽에 다 몰리도록 분할 되는 경우 • Θ(n2) IST (Information Sciences & Technology) Laboratory

  16. 힙 정렬 (1/5) 1 3 2 6 4 3 9 8 7 1 2 3 4 5 6 4 5 6 A IST (Information Sciences & Technology) Laboratory • 힙 • 이진 트리로 맨 아래 층을 제외하고는 완전히 채워져 있고 맨 아래층은 왼쪽부터 꽉 채워져 있음 조건 • 각 노드의 값은 자신의 자식의 값보다 작음 힙 만들기

  17. 힙 정렬 (2/5) buildHeap(A[ ], n) { fori ← n/2downto 1 heapify(A, i, n) } heapify(A[ ], k, n) { left ← 2k; right ← 2k+1; if(right ≤ n) then{ if(A[left] < A[right]) then smaller ← left; else smaller ← right; } else if (left ≤ n) then smaller ← left; else return; if(A[smaller] < A[k]) then{ A[k] ↔ A[smaller]; heapify(A, smaller, n); } } 1 3 2 6 4 3 9 8 7 4 5 6 힙 만들기 알고리즘 IST (Information Sciences & Technology) Laboratory

  18. 힙 정렬 (3/5) (a) (c) (b) 3 4 7 3 제거 4 6 7 6 4 6 3 9 9 8 7 9 8 8 3 4 제거 6 (f) 6 9 (e) (d) 7 8 7 9 7 6 3 9 4 3 8 4 3 8 4 IST (Information Sciences & Technology) Laboratory heapSort() 함수

  19. 힙 정렬 (4/5) 6 제거 (h) 9 7 9 7 제거 (i) (g) 7 8 9 8 7 8 3 3 6 4 6 4 3 6 4 (j) 9 8 제거 8 (k) 8 7 7 9 3 6 4 3 6 4 IST (Information Sciences & Technology) Laboratory heapSort() 함수

  20. 힙 정렬 (5/5) 1 2 3 4 5 6 A heapSort(A[ ], n) { buildHeap(A, n); ▷ 힙 만들기 fori ← ndownto 2 { A[1] ↔ A[i]; ▷ 교환 heapify(A, 1, i-1); } } 1 3 2 3 6 4 8 9 7 4 5 6 • heapSort() 함수 • 수행 시간 • Θ(nlogn) IST (Information Sciences & Technology) Laboratory

  21. 감사합니다천주희wngml1205@kunsan.ac.kr IST (Information Sciences & Technology) Laboratory

More Related