Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7

Similar presentations


Presentation on theme: "CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7"— Presentation transcript:

1 CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7
Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics 1

2 Izmir University of Economics
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(N2) worst-case performance, but this can be made exponentially unlikely with a little effort. Izmir University of Economics

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: S1={xєS-{v}| x<=v} and S2={xєS-{v}| x>=v} 4. return {quicksort(S1) {v}, quicksort(S2)} } Partition step ambiguous for elements equal to the pivot. Izmir University of Economics

4 Izmir University of Economics
Picking the Pivot Izmir University of Economics

5 Picking the Pivot Median-of-Three Partitioning
The median of a group of N numbers is the floor(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 left=8, right=0, center=6 (= floor((left+right)/2) ) Izmir University of Economics

6 Partitioning Strategy
First step: get the pivot element out of the way by swapping it with the last element. Middle element is 6, hence, the pivot. i starts at the first element and j starts at the next-to-last element. 8 1 4 9 6 3 5 2 7 left pivot center right Izmir University of Economics

7 Partitioning Strategy – Example (1)
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. 8 1 4 9 3 5 2 7 6 i j 8 1 4 9 3 5 2 7 6 i j Izmir University of Economics

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

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

10 Izmir University of Economics
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

11 Quicksort – Implementation (1)
typedef int ElementType; void Swap( ElementType *Lhs, ElementType *Rhs ) { ElementType Tmp = *Lhs; *Lhs = *Rhs; *Rhs = Tmp; } void Quicksort( ElementType A[ ], int N ) Qsort( A, 0, N - 1 ); Izmir University of Economics

12 Quicksort – Implementation (2)
/* Return median of Left, Center, and Right */ /* Order these and hide the pivot */ ElementType Median3( ElementType A[ ], int Left, int Right ) { int Center = ( Left + Right ) / 2; if( A[ Left ] > A[ Center ] ) Swap( &A[ Left ], &A[ Center ] ); if( A[ Left ] > A[ Right ] ) Swap( &A[ Left ], &A[ Right ] ); if( A[ Center ] > A[ Right ] ) Swap( &A[ Center ], &A[ Right ] ); /* Invariant: A[ Left ] <= A[ Center ] <= A[ Right ] */ Swap( &A[ Center ], &A[ Right - 1 ] ); /* Hide pivot */ return A[ Right - 1 ]; /* Return pivot */ } Izmir University of Economics

13 Quicksort – Implementation (3)
#define Cutoff ( 10 ) void Qsort( ElementType A[ ], int Left, int Right ) { int i, j; ElementType Pivot; if( Left + Cutoff <= Right ) { Pivot = Median3( A, Left, Right ); i = Left; j = Right - 1; for( ; ; ) { while( A[ ++i ] < Pivot ){ } while( A[ --j ] > Pivot ){ } if( i < j ) Swap( &A[ i ], &A[ j ] ); else break; } Swap( &A[ i ], &A[ Right - 1 ] ); /* Restore pivot */ Qsort( A, Left, i - 1 ); Qsort( A, i + 1, Right ); else /* Do an insertion sort on the subarray */ InsertionSort( A + Left, Right - Left + 1 ); Izmir University of Economics

14 Izmir University of Economics
Quicksort – Be Careful This is not correct i = Left + 1; j = Right - 2; for( ; ; ) { while( A[ i ] < Pivot ) i++; while( A[ j ] > Pivot ) j--; if( i < j ) Swap( &A[ i ], &A[ j ] ); else break; } Izmir University of Economics

15 Izmir University of Economics
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 = |S1| is the number of elements in S1. Worst-case, Best-case, Average-Case analyses will be performed. Izmir University of Economics

16 Izmir University of Economics
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 T(N)=T(1)+c = O(N2) Izmir University of Economics

17 Izmir University of Economics
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

18 Average-Case Analysis
Assume each of the sizes for S1is equally likely and hence probability is 1/N. Subtract (2) from (1) Divide both sides by N(N+1) Now we can telescope adding all equations Izmir University of Economics


Download ppt "CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7"

Similar presentations


Ads by Google