computer algorithms c++ - kocwcontents.kocw.net/kocw/document/2014/chungbuk/leechungse/... · 2016....

17
Quick Sort

Upload: others

Post on 30-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

Quick Sort

Page 2: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

Quick Sort

Page 3: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

Quick Sort

피봇에 의한 분할(partition)과정의 예

퀵 정렬 과정의 예

Page 4: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

Quick Sort

• 알고리즘 분석 – Worst case

시간복잡도 :

Page 5: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

Quick Sort • 알고리즘 분석

– average case

Page 6: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

• CA(0)=CA(1)=0임을 이용하여 위에 제시한 순환관계식을 정리하면 다음과 같다.

– 양변에 n을 곱하면,

– 위의 식에서 n에 n-1을 대입하면 다음과 같다.

– 여기서, 식(1)-식(2)를 계산하여 정리하면 다음과 같다.

식(1)

식(2)

시간복잡도 :

Page 7: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

Randomized Sorting Algorithms • void RQuickSort ( int p , int q )

// 전체 원소 a[1: n]을 오름차순으로 정렬. { if ( p < q ) { // 첫번째 원소와 임의의 위치의 원소와 교환 if ( (q-p) > 5 ) Interchange ( a ,random()%(q-p+1)+p, p ); // j는 주어진 입력을 분할하는 위치이다 int j = Partition ( p , q+1 ) ; RQuickSort ( p , j-1 ) ; RQuickSort ( j+1 , q ) ; } }

• Randomized 퀵 정렬의 순환 관계

– 이것은 퀵 정렬의 평균적인 경우의 순환 관계와 같음을 알 수 있다. 그러므로, 시간복잡도는 Ο(nlogn)이다.

Page 8: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

난수에 의한 퀵정렬

10, 9, 8, 7, 6, 5, 4, 3, 2, 1 : random(10)=5

Page 9: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

Selection

void Select1 ( Type a[] , int n , int k ) // a[1:n]에서 k번째 작은 값을 찾아서 a[]의 k번째 위치에 놓는다. // 남아있는 원소들은 다음 조건을 만족하도록 재배치된다. // a[m] <= a[k] for a <= m < k과 // a[m] >= a[k] for k < m <= n { int low = 1, up = n+1 ; a[n+1] = INFTY ; // a[n+1]은 무한 집합이다. do { // 1 <= low <= k <= up <= n+1 int j = Partition ( a , low , up ) ; // j는 a[j]가 a[]에서 j번째 작은 값을 만족하는 위치이다. if ( k == j ) return ; else if ( k < j ) up = j ; // j는 새로운 상한 값이다. else low = j+1 ; // (j+1)은 새로운 하한 값이다. } while ( TRUE ) ; }

Program 3.17 Finding the kth-smallest element

Page 10: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

Selection • TA

k(n)을 배열 a[1:n]에서 k번째로 작은 원소를 찾는 데 걸리는 평균 시간이라고 정의하자. 또한 평균적인 시간복잡도를 구하기 위하여 R(n)을 다음과 같이 정의하겠다

– 여기서 TA(n)은 Select1의 평균 계산 시간임을 알 수 있다. 그리고 TA(n) ≤ R(n)이라는 사실을 이용하여 지금부터는 TA(n) = Ο(n)이 성립함을 살펴보기로 하자.

Page 11: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

Selection • Theorem 3.3 The average computing time TA(n) of Select1 is Ο(n) - proof

Page 12: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

ⅰ) for n=2

ⅱ) assume R(n)≤4cn for all 2≤n<m, for n=m

Non-deacreasing function of n,

만약 m이 짝수이면 k=m/2일 때, m이 홀수이면 k=(m+1)/2일 때, 위의 식은 최대가 됨

Page 13: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

Strassen’s Matrix Multiplication

i)Divide & Conquer에 의한 행렬의 곱셈

(Let A and B be two nxn matrices)

시간복잡도 : T(n)=Ο(n3)

Page 14: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

Strassen’s Matrix Multiplication

ii)Strassen의 행렬 곱셈

Page 15: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

15

Now, let n = # of bits in each integer (X and Y)

X X = A2 + B Y Y = C2 + D

bits bits

XY = AC 2n + (AD + BC) 2 + BD How many operations? ∴ T(n) = 4T( ) + c·n T(1) = 1 T(n) = O(n2) So what?

A B C D

2n

2n

2n

2n

2n

2n

Page 16: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

16

Theorem: Let a, b, c, d ∈R. Then, the solution to the the recurrence equation b if n =1, T(n) = aT(n/c) + dn if n > 1,

where n is is O(n) if a<c T(n) = O(nlogn) If a=c

O(n ) if a>c [Proof] Exercise. Homework !!! Theorem is true even if n ≠ ck Why? Homework!!! Hint : ck ≤ n ≤ ck+1 for some k ∈ N. T(n) is increasing monotonically.

a power of c

aclog

note

(n=ck)

Page 17: Computer Algorithms C++ - KOCWcontents.kocw.net/KOCW/document/2014/Chungbuk/LeeChungse/... · 2016. 9. 9. · Computer Algorithms C++ Author: 김학원 Created Date: 8/29/2014 10:47:04

17

XY = AC 2n + (AD + BC) 2 + BD T(n) = 4T(n/2) + dn T(1) = 1 a = 4 > c = 2 ∴ T(n) = O(n ) = O(n2) How can we reduce a? AD + BC = (A - B) (D - C) + AC + BD

∴ XY = AC 2n +{(A - B) (D - C) + AC + BD}2 + BD T(n) = 3T( ) + d0n T(1) = 1 T(n) = O(n ) = O(n1.59)

2n

42log

2n

2n

32log