Dr.Surasak Mungsing E-mail: [email protected]

46
CSC 201 CSC 201 Analysis and Design of Analysis and Design of Algorithms Algorithms Lecture 05: Lecture 05: Analysis of time Complexity Analysis of time Complexity of Sorting Algorithms of Sorting Algorithms Dr.Surasak Mungsing Dr.Surasak Mungsing E-mail: [email protected] 07/04/22 1

description

CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms. Dr.Surasak Mungsing E-mail: [email protected]. Sorting Algorithms. Bin Sort Radix Sort Insertion Sort Shell Sort Selection Sort Heap Sort Bubble Sort Quick Sort Merge Sort. D. - PowerPoint PPT Presentation

Transcript of Dr.Surasak Mungsing E-mail: [email protected]

Page 1: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

CSC 201CSC 201Analysis and Design of AlgorithmsAnalysis and Design of Algorithms

Lecture 05:Lecture 05:Analysis of time Complexity of Analysis of time Complexity of Sorting AlgorithmsSorting Algorithms

Dr.Surasak MungsingDr.Surasak MungsingE-mail: [email protected]

04/24/23 1

Page 2: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/232

Sorting AlgorithmsSorting Algorithms Bin Sort Radix Sort Insertion Sort Shell Sort Selection Sort Heap Sort Bubble Sort Quick Sort Merge Sort

Page 3: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/233

Page 4: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/234

Bin SortBin Sort

A 2 C 5B 4 J 3H 3 I 4D 4 E 3 F 0 G 4

F A EHJ

BDGI

CBin 0 Bin 1 Bin 2 Bin 3 Bin 4 Bin 5

F 0 E 3A 2 C 5G 4 I 4H 3 J 3 B 4 D 4

(a) Input chain

(b) Nodes in bins

(c) Sorted chain

Page 5: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 5

Radix Sort with r=10 and d=3Radix Sort with r=10 and d=3

216 521 425 116 91 515 124 34 96 24(a) Input chain

24 34 91 96 116 124 216 425 515 521(d) Chain after sorting on most significant digit

515 216 116 521 124 24 425 34 91 96(c) Chain after sorting on second-least significant digit

521 91 124 34 24 425 515 216 116 96(b) Chain after sorting on least significant digit

Page 6: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 6

Insertion Sort Concept

Page 7: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/237

Page 8: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/238

Shell Sort Algorithm

Page 9: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/239

Shell Sort Algorithm

Page 10: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2310

Shell Sort Algorithm

Page 11: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2311

Page 12: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 12

Shell Sort Demohttp://e-learning.mfu.ac.th/mflu/1302251/demodemo//Chap03Chap03//ShellSortShellSort//

ShellSortShellSort..htmlhtml

Page 13: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2313

Selection Sort Concept

Page 14: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 14

Page 15: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2315

Heap Sort Algorithm

Page 16: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2316

Page 17: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 17

Bubble Sort Concept

Page 18: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 18

Page 19: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2319

Quick sortQuick sort

4313

8131

92

5765

75

260

4313

8131

92

5765

75

260

650

3113

2657

4392 75

81

Select pivot

Partition

The fastest known sorting algorithm in practice.

Page 20: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2320

Quick sortQuick sort

0 3126 5743 9275 816513

650

3113

2657

4392 75

81

0 3126 574313 65 9275 81

Quick sort small Quick sort large

Page 21: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 21

Quick sort

Page 22: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2322

Quick Sort Partitions

Page 23: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2323

Quick sort

Page 24: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2324

External Sort: A simple merge

Page 25: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2325

Merge Sort

Page 26: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2326

Merge Sort

Page 27: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2327

Page 28: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 28

Page 29: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2329

Analysis of Insertion SortAnalysis of Insertion Sort

for (int i = 1; i < a.length; i++){// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t;}

Page 30: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2330

ComplexityComplexity

Space/Memory Time

Count a particular operation Count number of steps Asymptotic complexity

Page 31: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2331

Comparison CountComparison Count

for (int i = 1; i < a.length; i++){// insert a[i] into a[0:i-1] int t = a[i]; int j; for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; a[j + 1] = t;}

Page 32: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2332

Comparison CountComparison Count

for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j];

How many comparisons are made?

Page 33: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2333

Comparison CountComparison Count

for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j];

number of compares depends on a[]s and t as well as on i

Page 34: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2334

Comparison CountComparison Count

Worst-case count = maximum countBest-case count = minimum countAverage count

Page 35: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2335

Worst-Case Comparison CountWorst-Case Comparison Count

for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j];

a = [1, 2, 3, 4] and t = 0 4 comparesa = [1,2,3,…,i] and t = 0 i compares

Page 36: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2336

Worst-Case Comparison CountWorst-Case Comparison Countfor (int i = 1; i < n; i++) for (j = i - 1; j >= 0 && t <

a[j]; j--) a[j + 1] = a[j];

total compares = 1 + 2 + 3 + … + (n-1)

= (n-1)n/2

= O(n2)

n

(i-1)i=2

T(n) =

Page 37: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2337

Average-Case Comparison Average-Case Comparison CountCount

for (int i = 1; i < n; i++) for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j];

n

i=2

T =i-12

= O(n2)

Page 38: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2338

4313

8131

92

5765

75

260

4313

8131

92

5765

75

260

650

3113

2657

4392 75

81

Select pivot

Partition

Analysis of Quick Sort

Page 39: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2339

MainMainQuick SortQuick Sort

RoutineRoutine private static void quicksort( Comparable [ ] a, int left, int right ) {/* 1*/ if( left + CUTOFF <= right ) {/* 2*/ Comparable pivot = median3( a, left, right ); // Begin partitioning/* 3*/ int i = left, j = right - 1;/* 4*/ for( ; ; ) {/* 5*/ while( a[ ++i ].compareTo( pivot ) < 0 ) { }/* 6*/ while( a[ --j ].compareTo( pivot ) > 0 ) { }/* 7*/ if( i < j )/* 8*/ swapReferences( a, i, j ); else/* 9*/ break; }/*10*/ swapReferences( a, i, right - 1 ); // Restore pivot/*11*/ quicksort( a, left, i - 1 ); // Sort small elements/*12*/ quicksort( a, i + 1, right ); // Sort large elements } else // Do an insertion sort on the subarray/*13*/ insertionSort( a, left, right ); }

Page 40: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2340

Worst-Case AnalysisWorst-Case Analysisเวลาท่ีใชใ้นการ run quick sort เท่ากับเวลาท่ีใชใ้นการทำา recursive call 2 ครัง้ + linear time ท่ีใชใ้นการเลือก pivot ซึ่งทำาให ้basic quick sort relation เท่ากับ

T(n) = T(i) + T(n-i-1) + cnในกรณี Worst- case เชน่ การท่ี pivot มค่ีาน้อยท่ีสดุเสมอ เวลาท่ีใชใ้นการทำา recursion คือ

T(n) = T(n-1) + cn n>1T(n-1)= T(n-2)+c(n-1)T(n-2)= T(n-3)+c(n-2)

… T(2) = T(1)+c(2)

รวมเวลาทัง้หมดT(n) = T(1) + c

n

i=2i = O(n2)

Page 41: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2341

The pivot is in the middle; T(n) = 2 T(n/2) + cn

Divide both sides by n;

Add all equations;

BestBest-Case Analysis-Case Analysis

T(n/2)

n/2

T(n)

n= + c

T(2)

2

T(1)

1= + c

T(n/4)

n/4

T(n/2)n/2

= + c

T(n/4)

n/4

T(n/8)

n/8= + c

T(n)

n

T(1)

1= + c log n

T(n) = c n logn + n = O(n log n)

Page 42: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2342

AverageAverage-Case Analysis (1/4)-Case Analysis (1/4)

2N

N -1

j=0

T( j ) + cNT(N) =

NT(N) =N -1

j=0

T( j ) + cN22

(N-1) T(N-1) = 2 + c(N – 1)2T( j )N -2

j=0

Average time of T(i) and T(N-i-1) is 1N

N -1

j=0

T( j )

Total time; T(N) = T(i) + T(N-i-1) + c N …………..(1)

Therefore

…………..(2)

…………..(3)

…………..(4)

…………..(5)

Page 43: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2343

AverageAverage-Case Analysis-Case Analysis (2/4)(2/4)

…………..(6)NT(N) – (N-1) T(N-1) = 2T(N-1) +2cN - c(5) – (4);

(7) divides by N(N+1);T(N)

N+1

T(N-1)

N

2c

N+1= + …………..(8)

Rearrange terms in equation and ignore c on the right-hand side;

NT(N) = (N+1) T(N-1) +2cN …………..(7)

Page 44: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2344

AverageAverage-Case Analysis-Case Analysis (3/4)(3/4)T(N)

N+1

T(N-1)

N

2c

N+1= +

T(N-2)

N-1

T(N-3)

N-2

2c

N-1= +

T(N-1)

N

T(N-2)

N-1

2c

N= +

…………..(8)

…………..(9)

…………..(10)...

T(2)

3

T(1)

2

2c

3= + …………..(11)

Sun equations (8) to (11); …………..(12)N +1

i=3

T(N)

N+1

T(1)

22c= +

Page 45: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/2345

AverageAverage-Case Analysis-Case Analysis (4/4)(4/4)

N +1

i=3

T(N)

N+1

T(1)

22c= + …………..(12)

Sum in equation (12) ia approximately logC(N+1)+ - 3/2, which is Euler’s constant 0.577

T(N) = O(Nlog N)

…………..(13)

and

T(N)

N+1= O(log N)therefore

…………..(14)

In summary, time complexity of Quick sort algorithm for Average-Case is

T(n) = O(n log n)

Page 46: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

Apr 24, 2023 46