CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting...

46
CSE 221/ICT221 CSE 221/ICT221 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] 06/16/22 1

Transcript of CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting...

Page 1: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

CSE 221/ICT221 CSE 221/ICT221 Analysis 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/19/23 1

Page 2: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/232

Sorting AlgorithmsSorting Algorithms

Bin Sort Radix Sort Insertion Sort Shell Sort Selection Sort Heap Sort Bubble Sort Quick Sort Merge Sort

CSE221/ICT221 Analysis and Design of Algorithms

Page 3: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/233

CSE221/ICT221 Analysis and Design of Algorithms

Page 4: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/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

CSE221/ICT221 Analysis and Design of Algorithms

Page 5: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/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: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/23 6

Insertion Sort Concept

Page 7: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/237

CSE221/ICT221 Analysis and Design of Algorithms

Page 8: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/238

Shell Sort Algorithm

CSE221/ICT221 Analysis and Design of Algorithms

Page 9: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/239

Shell Sort Algorithm

CSE221/ICT221 Analysis and Design of Algorithms

Page 10: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2310

Shell Sort Algorithm

CSE221/ICT221 Analysis and Design of Algorithms

Page 11: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2311

CSE221/ICT221 Analysis and Design of Algorithms

Page 12: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/23 12

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

ShellSortShellSort..htmlhtml

Page 13: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2313

Selection Sort Concept

CSE221/ICT221 Analysis and Design of Algorithms

Page 14: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/23 14

Page 15: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2315

Heap Sort Algorithm

CSE221/ICT221 Analysis and Design of Algorithms

Page 16: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2316

CSE221/ICT221 Analysis and Design of Algorithms

Page 17: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/23 17

Bubble Sort Concept

Page 18: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/23 18

Page 19: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2319

Quick sortQuick sort

4313

8131

92

57

65

75

260

4313

8131

92

57

65

75

260

650

3113

26

5743

92 75

81

Select pivot

Partition

The fastest known sorting algorithm in practice.

CSE221/ICT221 Analysis and Design of Algorithms

Page 20: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2320

Quick sortQuick sort

0 3126 5743 9275 816513

650

3113

26

5743

92 75

81

0 3126 574313 65 9275 81

Quick sort small Quick sort large

CSE221/ICT221 Analysis and Design of Algorithms

Page 21: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/23 21

Quick sort

Page 22: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2322

Quick Sort Partitions

CSE221/ICT221 Analysis and Design of Algorithms

Page 23: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2323

Quick sort

CSE221/ICT221 Analysis and Design of Algorithms

Page 24: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2324

External Sort: A simple merge

CSE221/ICT221 Analysis and Design of Algorithms

Page 25: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2325

Merge Sort

CSE221/ICT221 Analysis and Design of Algorithms

Page 26: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2326

Merge Sort

CSE221/ICT221 Analysis and Design of Algorithms

Page 27: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2327

CSE221/ICT221 Analysis and Design of Algorithms

Page 28: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/23 28

Page 29: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/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;}

CSE221/ICT221 Analysis and Design of Algorithms

Page 30: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2330

ComplexityComplexity

Space/Memory Time

Count a particular operation Count number of steps Asymptotic complexity

CSE221/ICT221 Analysis and Design of Algorithms

Page 31: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/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;}

CSE221/ICT221 Analysis and Design of Algorithms

Page 32: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2332

Comparison CountComparison Count

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

How many comparisons are made?

CSE221/ICT221 Analysis and Design of Algorithms

Page 33: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/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

CSE221/ICT221 Analysis and Design of Algorithms

Page 34: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2334

Comparison CountComparison Count

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

CSE221/ICT221 Analysis and Design of Algorithms

Page 35: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/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 compares

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

CSE221/ICT221 Analysis and Design of Algorithms

Page 36: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/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) =

CSE221/ICT221 Analysis and Design of Algorithms

Page 37: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/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-1

2

= O(n2)

CSE221/ICT221 Analysis and Design of Algorithms

Page 38: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2338

4313

8131

92

57

65

75

260

4313

8131

92

57

65

75

260

650

3113

26

5743

92 75

81

Select pivot

Partition

Analysis of Quick Sort

CSE221/ICT221 Analysis and Design of Algorithms

Page 39: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/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 ); }

CSE221/ICT221 Analysis and Design of Algorithms

Page 40: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/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)

CSE221/ICT221 Analysis and Design of Algorithms

Page 41: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/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)

CSE221/ICT221 Analysis and Design of Algorithms

Page 42: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/2342

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

2

N

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 1

N

N -1

j=0

T( j )

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

Therefore

…………..(2)

…………..(3)

…………..(4)

…………..(5)

CSE221/ICT221 Analysis and Design of Algorithms

Page 43: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/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)

CSE221/ICT221 Analysis and Design of Algorithms

Page 44: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/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= +

CSE221/ICT221 Analysis and Design of Algorithms

Page 45: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

04/19/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)

CSE221/ICT221 Analysis and Design of Algorithms

Page 46: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th.

Apr 19, 2023 46