Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 05: การวิเคราะห์ความซับซ้อนของ ขั้นตอนวิธีการเรียงลำดับข้อมูล Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th May-19
Sorting Algorithms Bin Sort Radix Sort Insertion Sort Shell Sort Selection Sort Heap Sort Bubble Sort Quick Sort Merge Sort 5/5/2019
5/5/2019
Bin Sort A 2 C 5 B 4 J 3 H I D E F G F A E H J B D G I C F E 3 A 2 C 5 G (a) Input chain F A E H J B D G I C Bin 0 Bin 1 Bin 2 Bin 3 Bin 4 Bin 5 (b) Nodes in bins F E 3 A 2 C 5 G 4 I H J B D (c) Sorted chain 5/5/2019
Radix Sort with r=10 and d=3 216 521 425 116 91 515 124 34 96 24 (a) Input chain 521 91 124 34 24 425 515 216 116 96 (b) Chain after sorting on least significant digit 515 216 116 521 124 24 425 34 91 96 (c) Chain after sorting on second-least significant digit 24 34 91 96 116 124 216 425 515 521 (d) Chain after sorting on most significant digit 5/5/2019
Insertion Sort Concept 5/5/2019
Insertion Sort Algorithm 5/5/2019
Shell Sort Algorithm 5/5/2019
Shell Sort Algorithm 5/5/2019
Shell Sort Algorithm 5/5/2019
Shell Sort Algorithm 5/5/2019
Selection Sort Concept 5/5/2019
Selection Sort Algorithm 5/5/2019
Heap Sort Algorithm 5/5/2019
Heap Sort Algorithm 5/5/2019
Bubble Sort Concept 5/5/2019
Bubble Sort Algorithm 5/5/2019
Quick sort The fastest known sorting algorithm in practice. 31 75 81 43 13 81 31 92 57 65 75 26 Select pivot Partition 5/5/2019
Quick sort 31 26 57 43 92 75 81 65 13 Quick sort small 31 26 57 43 92 75 81 65 13 Quick sort small Quick sort large 5/5/2019
Quick sort 5/5/2019
Quick Sort Partitions 5/5/2019
Quick sort 5/5/2019
External Sort: A simple merge 5/5/2019
Merge Sort 5/5/2019
Merge Sort 5/5/2019
5/5/2019
Sort Algorithm Analysis 5/5/2019
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; } 5/5/2019
Comparison Count How many comparisons are made? 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; } How many comparisons are made? 5/5/2019
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 Worst-case count = maximum count Best-case count = minimum count Average count 5/5/2019
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 5/5/2019
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 (i-1) i=2 T(n) = = (n-1)n/2 = O(n2) 5/5/2019
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(n2) 5/5/2019
Analysis of Quick Sort 31 75 81 43 57 13 26 65 92 Select pivot Select pivot Partition 5/5/2019
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 ); Main Quick Sort Routine 5/5/2019
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) + 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 n i=2 i = O(n2) 5/5/2019
Best-Case Analysis … T(n) = c n logn + n = O(n log n) The pivot is in the middle; T(n) = 2 T(n/2) + cn Divide both sides by n; Add all equations; T(n/2) n/2 T(n) n = + c T(n/4) n/4 T(n/2) n/2 = + c T(n/4) n/4 T(n/8) n/8 = + c … T(2) 2 T(1) 1 = + c T(n) n T(1) 1 = + c log n T(n) = c n logn + n = O(n log n) 5/5/2019
Average-Case Analysis (1/4) เวลาที่ใช้ในการทำงานทั้งหมดคือ T(N) = T(i) + T(N-i-1) + c N …………..(1) เวลาทำงานเฉลี่ยของ T(i) และ T(N-i-1) คือ 1 N N -1 j=0 T( j ) …………..(2) ดังนั้น 2 N N -1 j=0 T( j ) + cN T(N) = …………..(3) NT(N) = N -1 j=0 T( j ) + cN2 2 …………..(4) (N-1) T(N-1) = 2 + c(N – 1)2 T( j ) N -2 j=0 …………..(5) 5/5/2019
Average-Case Analysis (2/4) (5) – (4); NT(N) – (N-1) T(N-1) = 2T(N-1) +2cN - c …………..(6) จัดเทอมใหม่และทิ้งค่า c ทางด้านขวาของสมการซึ่งไม่มีนัยสำคัญ ; NT(N) = (N+1) T(N-1) +2cN …………..(7) (7) หาร ด้วย N(N+1); T(N) N+1 T(N-1) N 2c = + …………..(8) 5/5/2019
Average-Case Analysis (3/4) T(N) N+1 T(N-1) N 2c = + T(N-2) N-1 T(N-3) N-2 2c = + T(N-1) N …………..(8) …………..(9) …………..(10) . T(2) 3 T(1) 2 …………..(11) N +1 i=3 T(N) N+1 T(1) 2 2c = + บวกสมการทั้งหมดตั้งแต่ (8) ถึง (11); …………..(12) 5/5/2019
Average-Case Analysis (4/4) i=3 T(N) N+1 T(1) 2 2c = + …………..(12) ผลบวกในสมการ (12) มีค่าประมาณ logC(N+1)+ - 3/2, ซึ่ง คือ Euler’s constant มีค่าประมาณ 0.577 = O(Nlog N) …………..(13) และ = O(log N) ดังนั้น …………..(14) สรุปได้ว่าความซับซ้อนของ Quick sort algorithm กรณี Average-Case คือ T(n) = O(n log n) 5/5/2019
Priority Queue Heap Sort 5/5/2019
Min Priority Queue Collection of elements. Each element has a priority or key. Supports following operations: isEmpty size add/put an element into the priority queue get element with min priority remove element with min priority May-19
Max Priority Queue Collection of elements. Each element has a priority or key. Supports following operations: isEmpty size add/put an element into the priority queue get element with max priority remove element with max priority May-19
Min Tree Example 2 4 9 3 4 8 7 9 9 Root has minimum element. May-19
Max Tree Example 9 4 9 8 4 2 7 3 1 Root has maximum element. May-19
Min Heap Definition complete binary tree min tree May-19
Heap Height What is the height of an n node heap ? Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1). May-19
A Heap Is Efficiently Represented As An Array 9 8 6 7 2 5 1 9 8 7 6 2 5 1 3 4 10 May-19
Complexity Of Operations Two good implementations are heaps and leftist trees. isEmpty, size, and get => O(1) time put and remove => O(log n) time where n is the size of the priority queue May-19
Practical Complexities 109 instructions/second Teraflop computer the 32yr time becomes approx 10 days. 5/5/2019
Impractical Complexities 109 instructions/second 5/5/2019
Summary 5/5/2019
Faster Computer Vs Better Algorithm Algorithmic improvement more useful than hardware improvement. E.g. 2n to n3 5/5/2019
การวิเคราะห์ขั้นตอนวิธีที่ใช้ในโครงสร้างข้อมูลแบบ List Next Lecture: การวิเคราะห์ขั้นตอนวิธีที่ใช้ในโครงสร้างข้อมูลแบบ List และ Stack และ Queues 5-May-19