1|void quicksort (int[] a, int lo, int hi)2|{3| int i=lo, j=hi, h;4| int x=a[k];// partition5| do6| {7| while (a[i]x) j--;9| if (i
6 5 1 26|8 3 6 4 9 5 71|2|3|4|5|<strong>Sort</strong>ed11<strong>Sort</strong>ed<strong>Sort</strong>ed16 5 1 2<strong>Sort</strong>ed1Pivot ItemPivot Item222<strong>Sort</strong>edPivot Item6 5 25556<strong>Sort</strong>ed6<strong>Sort</strong>ed67| 8 3 6 4 9 5 78|9|10|11|<strong>Sort</strong>ed3<strong>Sort</strong>ed3Pivot Item5 3 43<strong>Sort</strong>ed6Pivot Item <strong>Sort</strong>edFigure 4.Best Case <strong>of</strong> <strong>Quick</strong>sort9 8 7<strong>Sort</strong>ed5 4 6 7 8 9sorted4Pivot Item455<strong>Sort</strong>ed6<strong>Sort</strong>ed6<strong>Sort</strong>ed7Pivot ItemPivot Item<strong>Sort</strong>ed87 8 99Figure 3.Worst Case <strong>of</strong> <strong>Quick</strong>sortThe worst case <strong>of</strong> quick sort occur when we choose the smallestor the largest element as the pivot. It needs the mostcomparisons in each iteration. See figure 3, we want to sorta=[6,5,1,2] . From line1 to 2, the smallest item 1 is chosen as thepivot. All elements need to compare with the pivot item once.From line 2 to 3, the largest item 6 is chosen as the pivot. Allelements need to compare with the pivot item once. From line 3to 4, 1 comparison is needed.Let T(N) be the number <strong>of</strong> comparison needed to sort an array<strong>of</strong> N elements.In the worst case, we need N-1 comparisons in the first round.Then we need T(N-1) comparisons to sort N-1 elements.TNN‐1TN‐11We need 0 time to sort 1 element:T102The best case <strong>of</strong> quicksort occurs when we choose median as thepivot item each iteration. Figure 4 shows a example <strong>of</strong> the bestcase <strong>of</strong> <strong>Quick</strong>sort. Each partition decreases by almost half thearray size each iteration. From line 7 to line8, after 6 timescomparison, 6 is in the right place and we get partition {5,3,4}and {9,8,7}. It is the same with line 8-9. Line 10, when there aretwo elements left, the array is sorted.Let T(N) be the number <strong>of</strong> comparison needed to sort an array<strong>of</strong> N elements. In the best case, we need N-1 comparisons tosort the pivot in the first iteration. We need T((N-1)/2)comparisons to sort each two partition.T(N)=N-1 + 2T((N-1)/2) [1]We need 0 comparisons to sort 1 element.T(1)=0 [2]TN‐1N‐2TN‐2TN‐2N‐3TN‐3…TN‐N‐1N‐N‐1TN‐N‐11T01T(N)=(N-1)+T(N-1)=(N-1)+(N-2)+ TN‐2(N-1)+(N-2)+(N-3)+ TN‐3(N-1)+(N-2)+(N-3)+…+T(N-(N-1))=(N-1)+(N-2)+(N-3)+…+1=(N 2 +N)/2=O(N 2 )Thus, we have a O(N 2 ) time complexity in the worst case.345T((N-1)/2)=2T((N-3)/4)+(N-1)/2-1 [3]Equation [3] replacing T((N-1)/2)) in equation [1] yieldsT(N)=2[(2T((N-3)/4)+(N-1)/2-1)]+N-1=2 2 T((N-3)/2 2 )+N-3+N-1 [4]T((N-3)/2 2 )=2T((N-7)/2 3 )+(N-3)/4-1 [5]Equation [5] replacing T((N-3)/2 2 ) in equation [4] yieldsT(N)=2 3 T((N-7)/2 3 )+N-7+N-3+N-1 [6]T(N)=2 k T(N-1+N-3+N-7+…+N-(2 k -1))5.Empirical EvaluationThe efficiency <strong>of</strong> the <strong>Quick</strong>sort algorithm will be measured inCPU time which is measured using the system clock on amachine with minimal background processes running, withrespect to the size <strong>of</strong> the input array, and compared to theMergesort algorithm. <strong>Quick</strong> sort algorithm will be run with thearray size parameter set to: 1k, 2k,…10k. To ensurereproducibility, all datasets and algorithms used in this