Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing

Similar presentations


Presentation on theme: "CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing"— Presentation transcript:

1

2 CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th Jan-161

3 1/31/2016 2 Sorting Algorithms  Bin Sort  Radix Sort  Insertion Sort  Shell Sort  Selection Sort  Heap Sort  Bubble Sort  Quick Sort  Merge Sort

4 1/31/2016 3

5 4 Bin Sort A2C5B4J3H3I4D4E3F0G4 FAE H J B D G I C Bin 0Bin 1Bin 2Bin 3Bin 4Bin 5 F0E3A2C5G4I4H3J3B4D4 (a) Input chain (b) Nodes in bins (c) Sorted chain

6 1/31/20165 Radix Sort with r=10 and d=3 21652142511691515124349624 (a) Input chain 24349196116124216425515521 (d) Chain after sorting on most significant digit 51551521621611611652112424425349196 (c) Chain after sorting on second-least significant digit 5219191124342442551521611696 (b) Chain after sorting on least significant digit

7 1/31/20166 Insertion Sort Concept

8 1/31/2016 7

9 8 Shell Sort Algorithm

10 1/31/2016 9 Shell Sort Algorithm

11 1/31/2016 10 Shell Sort Algorithm

12 1/31/2016 11

13 1/31/201612 Shell Sort Demo http://e-learning.mfu.ac.th/mflu/1302251/demo/Chap03/ShellSort/ShellSort.html Shell Sort Demo http://e-learning.mfu.ac.th/mflu/1302251/demo/Chap03/ShellSort/ShellSort.html

14 1/31/2016 13 Selection Sort Concept

15 1/31/201614

16 1/31/2016 15 Heap Sort Algorithm

17 1/31/2016 16

18 1/31/201617 Bubble Sort Concept

19 1/31/201618

20 1/31/2016 19 Quick sort 43 13 81 31 92 57 65 75 26 0 43 13 81 31 92 57 65 75 26 0 65 0 31 13 26 57 43 92 75 81 Select pivot Partition The fastest known sorting algorithm in practice.

21 1/31/2016 20 Quick sort 0 3126 57 43 92 75 81 65 13 65 0 31 13 26 57 43 92 75 81 0 3126 57 43 13 65 92 75 81 Quick sort small Quick sort large

22 1/31/201621 Quick sort

23 1/31/2016 22 Quick Sort Partitions

24 1/31/2016 23 Quick sort

25 1/31/2016 24 External Sort: A simple merge

26 1/31/2016 25 Merge Sort

27 1/31/2016 26 Merge Sort

28 1/31/2016 27

29 1/31/201628

30 1/31/2016 29 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; }

31 1/31/2016 30 Complexity  Space/Memory  Time  Count a particular operation  Count number of steps  Asymptotic complexity

32 1/31/2016 31 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; }

33 1/31/2016 32 Comparison Count for (j = i - 1; j >= 0 && t < a[j]; j--) a[j + 1] = a[j]; How many comparisons are made?

34 1/31/2016 33 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

35 1/31/2016 34 Comparison Count  Worst-case count = maximum count  Best-case count = minimum count  Average count

36 1/31/2016 35 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

37 1/31/2016 36 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(n 2 ) n ( i-1 )  i=2 T(n) =

38 1/31/2016 37 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]; n  i=2 T = i-1 2 = O(n 2 )

39 1/31/2016 38 43 13 81 31 92 57 65 75 26 0 43 13 81 31 92 57 65 75 26 0 65 0 31 13 26 57 43 92 75 81 Select pivot Partition Analysis of Quick Sort

40 1/31/2016 39 Main Quick Sort Routine 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 ); }

41 1/31/2016 40 Worst-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) + cnn>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 n  i=2 i = O(n 2 )

42 1/31/2016 41 The pivot is in the middle;T(n) = 2 T(n/2) + cn Divide both sides by n; Add all equations; Best-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)

43 1/31/2016 42 Average-Case Analysis (1/4) 2 N N -1  j=0 T( j ) + cN T(N) = NT(N) = N -1  j=0 T( j ) + cN 2 2 (N-1) T(N-1) = 2 + c(N – 1) 2 T( 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)

44 1/31/2016 43 Average-Case Analysis (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)

45 1/31/2016 44 Average-Case Analysis (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) 2 2c = +

46 1/31/2016 45 Average-Case Analysis (4/4) N +1  i=3 T(N) N+1 T(1) 2 2c = + …………..(12) Sum in equation (12) ia approximately log C (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)

47 31-Jan-1646


Download ppt "CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing"

Similar presentations


Ads by Google