470 likes | 646 Views
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. Sorting Algorithms. Bin Sort Radix Sort Insertion Sort Shell Sort Selection Sort Heap Sort Bubble Sort Quick Sort
E N D
CSE 221/ICT221 Analysis and Design of AlgorithmsLecture 05:Analysis of time Complexity of Sorting Algorithms Dr.SurasakMungsing E-mail: Surasak.mu@spu.ac.th
Sorting 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
D H C J B C I B D E A G F E F G A H J I 4 0 4 2 3 3 4 4 4 4 4 5 5 3 0 2 3 4 3 3 I J G H D C F A E B Bin 0 Bin 1 Bin 2 Bin 3 Bin 4 Bin 5 Bin Sort (a) Input chain (b) Nodes in bins (c) Sorted chain CSE221/ICT221 Analysis and Design of Algorithms
216 521 425 116 91 515 124 34 96 24 24 521 515 216 91 34 116 124 91 34 96 521 124 24 116 425 124 24 425 216 515 216 34 425 116 91 515 521 96 96 (a) Input chain (b) Chain after sorting on least significant digit (c) Chain after sorting on second-least significant digit (d) Chain after sorting on most significant digit Radix Sort with r=10 and d=3
Shell Sort Algorithm CSE221/ICT221 Analysis and Design of Algorithms
Shell Sort Algorithm CSE221/ICT221 Analysis and Design of Algorithms
Shell Sort Algorithm CSE221/ICT221 Analysis and Design of Algorithms
Shell Sort Demohttp://e-learning.mfu.ac.th/mflu/1302251/demo/Chap03/ShellSort/ShellSort.html
Selection Sort Concept CSE221/ICT221 Analysis and Design of Algorithms
Heap Sort Algorithm CSE221/ICT221 Analysis and Design of Algorithms
31 75 81 57 43 0 13 26 65 92 Select pivot 31 75 81 57 43 0 13 26 65 92 Partition 31 75 92 57 13 65 43 81 0 26 Quick sort The fastest known sorting algorithm in practice. CSE221/ICT221 Analysis and Design of Algorithms
31 75 92 57 13 65 43 81 0 26 Quick sort small Quick sort large 13 43 26 31 0 57 75 65 81 92 26 31 13 0 43 75 57 65 81 92 Quick sort CSE221/ICT221 Analysis and Design of Algorithms
Quick Sort Partitions CSE221/ICT221 Analysis and Design of Algorithms
Quick sort CSE221/ICT221 Analysis and Design of Algorithms
External Sort: A simple merge CSE221/ICT221 Analysis and Design of Algorithms
Merge Sort CSE221/ICT221 Analysis and Design of Algorithms
Merge Sort CSE221/ICT221 Analysis and Design of Algorithms
Analysis 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
Complexity • Space/Memory • Time • Count a particular operation • Count number of steps • Asymptotic complexity CSE221/ICT221 Analysis and Design of Algorithms
Comparison 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
Comparison 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
Comparison 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
Comparison Count • Worst-case count = maximum count • Best-case count = minimum count • Average count CSE221/ICT221 Analysis and Design of Algorithms
Worst-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
n T(n) (i-1) = i=2 Worst-Case Comparison Count for (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) CSE221/ICT221 Analysis and Design of Algorithms
i-1 2 n = T i=2 Average-Case Comparison Count for (int i = 1; i < n; i++) for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; = O(n2) CSE221/ICT221 Analysis and Design of Algorithms
31 75 81 57 43 0 13 26 65 92 Select pivot 31 75 81 57 43 0 13 26 65 92 Partition 31 75 92 57 13 65 43 81 0 26 Analysis of Quick Sort CSE221/ICT221 Analysis and Design of Algorithms
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 ); } MainQuick SortRoutine CSE221/ICT221 Analysis and Design of Algorithms
n i i=2 Worst-Case Analysis เวลาที่ใช้ในการ runquick sortเท่ากับเวลาที่ใช้ในการทำrecursive call2 ครั้ง + linear timeที่ใช้ในการเลือกpivot ซึ่งทำให้basic quicksort relation เท่ากับT(n) = T(i) + T(n-i-1) + cn ในกรณีWorst- caseเช่น การที่pivotมีค่าน้อยที่สุดเสมอ เวลาที่ใช้ในการทำrecursion คือT(n) = T(n-1) + cn n>1 T(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 = O(n2) CSE221/ICT221 Analysis and Design of Algorithms
= + c = + c T(n/2) T(1) T(n/4) T(n/4) T(n/8) T(2) T(n) T(n/2) T(n) T(1) = + c n/2 n/4 n/4 n/8 n/2 1 2 n n 1 + c log n + c = = Best-Case Analysis The pivot is in the middle; T(n) = 2 T(n/2) + cn Divide both sides by n; Add all equations; … T(n) = c n logn + n = O(n log n) CSE221/ICT221 Analysis and Design of Algorithms
+ cN T(N) = Average timeofT(i)andT(N-i-1)is N -1 N -1 N -1 T( j ) T( j ) T( j ) j=0 j=0 j=0 + cN2 2 NT(N) = 2 (N-1) T(N-1) = + c(N – 1)2 1 2 T( j ) N N N -2 j=0 Average-Case Analysis (1/4) Total time; T(N) = T(i) + T(N-i-1) + c N…………..(1) …………..(2) Therefore …………..(3) …………..(4) …………..(5) CSE221/ICT221 Analysis and Design of Algorithms
Rearrange terms in equation and ignore c onthe right-hand side; NT(N) = (N+1) T(N-1) +2cN …………..(7) …………..(8) (7) divides by N(N+1); T(N-1) 2c T(N) + = N N+1 N+1 Average-Case Analysis(2/4) …………..(6) NT(N) – (N-1) T(N-1) = 2T(N-1) +2cN - c (5)– (4); CSE221/ICT221 Analysis and Design of Algorithms
…………..(8) …………..(9) …………..(10) . . . …………..(11) T(N-1) T(N-2) T(N-3) T(N-1) T(1) 2c 2c 2c 2c T(N-2) T(N) T(2) T(1) T(N) + + + + = = = = + = 2c N-1 N-2 N 2 N+1 N-1 N 3 N+1 N-1 N 3 2 N+1 N +1 i=3 Average-Case Analysis(3/4) Sun equations (8)to(11); …………..(12) CSE221/ICT221 Analysis and Design of Algorithms
…………..(12) Sum in equation (12)ia approximately logC(N+1)+ - 3/2, whichisEuler’s constant 0.577 …………..(13) therefore T(1) T(N) T(N) + = 2c and =O(Nlog N) T(N) …………..(14) =O(log N) 2 N+1 N+1 N +1 i=3 Average-Case Analysis(4/4) In summary, time complexity ofQuick sort algorithmforAverage-Case is T(n) = O(n log n) CSE221/ICT221 Analysis and Design of Algorithms