data structures and algorithms chapter 7 sorting
play

Data Structures and Algorithms Chapter 7: Sorting (Quicksort) Text: - PowerPoint PPT Presentation

CE 221 Data Structures and Algorithms Chapter 7: Sorting (Quicksort) Text: Read Weiss, 7.7 Izmir University of Economics 1 Quicksort As the name implies quicksort is the fastest known sorting algorithm in practice. Average running


  1. CE 221 Data Structures and Algorithms Chapter 7: Sorting (Quicksort) Text: Read Weiss, § 7.7 Izmir University of Economics 1

  2. Quicksort • As the name implies quicksort is the fastest known sorting algorithm in practice. • Average running time is O(NlogN). • Fast, due to a very tight and highly optimized inner loop. • O(N 2 ) worst-case performance, but this can be made exponentially unlikely with a little effort. Izmir University of Economics 2

  3. Quicksort – Basic Algorithm • Sorting an array S consists of 4 easy steps: quicksort (S) { 1. If the number of elements in S is 0 or 1, then return 2. Pick any element v in S . This is called the pivot . 3. Partition S-{v} into 2 disjoint groups: S 1 ={x є S-{v}| x<=v} and S 2 ={x є S-{v}| x>=v} 4. return {quicksort(S 1 ) {v}, quicksort(S 2 )} } • Partition step ambiguous for elements equal to the pivot. Izmir University of Economics 3

  4. Picking the Pivot Izmir University of Economics 4

  5. Picking the Pivot Median-of-Three Partitioning • The median of a group of N numbers is the ceiling(N/2) th largest number. Unfortunately, this is hard to calculate and would slow down the quicksort considerably. • A good estimate can be obtained by picking three elements randomly and using the median of these as pivot. • Common course is to use as pivot the median of the left, right and center elements. • Example: input: 8, 1, 4, 9, 6, 3, 5, 2, 7, 0 • leftmost element= 8, rightmost element = 0, • centermost element = 6 (at index floor( (left + right) / 2) ) Izmir University of Economics 5

  6. Partitioning Strategy • First step: sort these three elements and then get the pivot element out of the way by swapping it with the next-to-last element. 8 1 4 9 6 3 5 2 7 0 left center right 0 1 4 9 6 3 5 2 7 8 pivot left right center 0 1 4 9 7 3 5 2 6 8 left center pivot right Izmir University of Economics 6

  7. Partitioning Strategy – Example (1) • Index i starts at position ( left + 1 ) and j starts at ( right - 2 ). • move all the small elements to the left part of the array and all the large elements to the right part. Small and large are with respect to the pivot. • While i is to the left of j, move i right skipping over elements that are smaller than the pivot. We move j left, skipping over elements that are larger than the pivot. • When i and j have stopped, i is pointing at a large and j is pointing at a small element. If i is to the left of j, i and j are swapped. 0 1 4 9 7 3 5 2 6 8 i j 0 1 4 9 7 3 5 2 6 8 i j Izmir University of Economics 7

  8. Partitioning Strategy – Example (2) After first swap 0 1 4 2 7 3 5 9 6 8 i j 0 1 4 2 7 3 5 9 6 8 i j After second swap 0 1 4 2 5 3 7 9 6 8 i j Izmir University of Economics 8

  9. Partitioning Strategy – Example (3) Before third swap 0 1 4 2 5 3 7 9 6 8 j i • At this stage, i and j have crossed, so no swap is performed. The final part is to swap the pivot element with the element pointed to by i. After swap with the 0 1 4 2 5 3 6 9 7 8 pivot IMPORTANT: In case of equality both i and j will stop. Izmir University of Economics 9

  10. Small Arrays • For very small arrays (N <= 20), quicksort does not perform well as insertion sort. • Furthermore, because quicksort is recursive, these cases will occur frequently. A common solution is not to use quicksort recursively for small arrays, but instead use a sorting algorithm that is efficient for small arrays, such as insertion sort. • Using this strategy can actually save about 15% in the running time. Izmir University of Economics 10

  11. Quicksort – Implementation (1) public static <AnyType> void swapReferences(AnyType [] a, int index1, int index2) { AnyType tmp = a[ index1 ]; a[ index1 ] = a[ index2 ]; a[ index2 ] = tmp; } Izmir University of Economics 11

  12. Quicksort – Implementation (2) Izmir University of Economics 12

  13. Quicksort – Be Careful This is not correct i = left + 1, j = right - 2; for( ; ; ) { while( a[ i ].compareTo(pivot) < 0 ) i++; while( a[ j ].compareTo(pivot) > 0 ) j--; if( i < j ) swapReferences( a, i, j ); else break; } Izmir University of Economics 13

  14. Analysis of Quicksort • Assume a random pivot (no median-of-three partitioning), no cutoff – T(0) = T(1) = 1 – T(N) = T(i) + T(N-i-1) + cN where i = |S 1 | is the number of elements in S 1 . • Worst-case, Best-case, Average-Case analyses will be performed. Izmir University of Economics 14

  15. Worst-Case Analysis • If the pivot is the smallest element, all the time. Then i = 0 and if we ignore T(0)=1, – 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) • adding up all these equations N  • T(N)=T(1)+c = O(N 2 ) i  2 i Izmir University of Economics 15

  16. Best-Case Analysis • In the best case the pivot is in the middle. • T(N)=2T(N/2)+cN • Divide both sides of the equation by N – T(N)/N=T(N/2)/(N/2)+c – T(N/2)/(N/2)=T(N/4)/(N/4)+c – ... – T(2)/(2)=T(1)/(1)+c • adding up all the equations • T(N)/N= T(1)/1+clogN • T(N)=cNlogN+N=O(NlogN) Izmir University of Economics 16

  17. Average-Case Analysis • Assume each of the sizes for • Now we can telescope S 1 is equally likely and hence  T ( N ) T ( N 1 ) 2 c probability is 1/N.     N 1 N N 1  N 1 1           T ( N 1 ) T ( N 2 ) 2 c T ( N ) T ( i ) T ( N i 1 ) cN   N   i 0 N N 1 N    N 1 T ( N 2 ) T ( N 3 ) 2 c        2       NT ( N ) 2 T ( i ) cN ( 1 )    N 1 N 2 N 1  i 0   N 2       2  ( 1 ) ( 1 ) 2 ( ) ( 1 ) ( 2 ) N T N T i c N ( 2 ) ( 1 ) 2 T T c    i 0 3 2 3 • Subtract (2) from (1) • adding all equations        NT ( N ) ( N 1 ) T ( N 1 ) 2 T ( N 1 ) 2 cN c  1 N ( ) ( 1 ) 1 T N T        ( ) ( 1 ) ( 1 ) 2 NT N N T N cN 2 c  1 2 N i  i 3 • Divide both sides by N(N+1) T ( N ) T ( 1 )       3 2 (log ( 1 ) ) c N  e  2 N 1 2 T ( N ) T ( N 1 ) 2 c      ( ) ( log ) T N O N N N 1 N N 1 Izmir University of Economics 17

  18. Homework Assignments • 7.19, 7.20, 7.21, 7.22, 7.31, 7.44 (7.39 in 3/e), 7.45 (7.40 in 3/e), 7.54 (7.49 in 3/e), 7.55 (7.50 in 3/e) • You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics 18

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend