Sie sind auf Seite 1von 3

Quicksort

http://faculty.simpson.edu/lydia.sinapova/www/cmsc250/LN250_Trem...

CmSc 250 Fundamentals of Computing III

Sorting Algorithms - QUICKSORT


Example
if( left + 10 <= right) { int i = left, j = right - 1; for ( ; ; ) { while (a[++i] < pivot ) {} while (pivot < a[--j] ) {} if (i < j) swap (a[i],a[j]); else break; } swap (a[i], a[right-1); quicksort ( a, left, i-1); quicksort (a, i+1, right); else } insertionsort (a, left, right);

// move the left finger // move the right finger // swap // break if fingers have crossed // restore the pivot // call quicksort for the left part // call quicksort for the left part

Let's see how this code will work on an example, disregarding the first if statement - the check for the number of elements. Source array to be sorted: 10, 3, 25, 6, 8, 17, 1, 2, 18, 5 I. Compute the first partitioning: Source array: 10, 3, 25, 6, 8, 17, 1, 2, 18, 5 1. Choosing the pivot: The median-of-three: [10, 8, 5] : the median is 8 2. Order the three elements and hide the pivot : 5, 3, 25, 6, 8, 17, 1, 2, 18, 10 5, 3, 25, 6, 10, 17, 1, 2, 18, 8 3. Start rearranging: Set two indices: i and j i = left (i.e. i = 0) j = right (i.e. j = 9)

5, 3, 25, 6, 10, 17, 1, 2, 18, 8 ^ ^ i j

At each run of the for loop we swap two elements 3.1. First run: while (a[++i] < pivot ){} ++i means: first increment i, then perform the comparison. Move i to the right skipping elements that are less than the pivot We know that the very first element is less than the pivot, so we do not compare it

5, 3, 25, 6, 10, 17, 1, 2, 18, 8 ^ ^ i j

1 of 3

18/01/2013 22:47

Quicksort

http://faculty.simpson.edu/lydia.sinapova/www/cmsc250/LN250_Trem...

i stops at 25, as 25 is greater than the pivot ( i = 2) while (pivot < a[--j] ) {} Initially j is 'right' = 9. This is the last element, and there is the pivot. We do not process the pivot, that is why we write --j - first decrement, and then compare.
5, 3, 25, 6, 10, 17, 1, 2, 18, 8 ^ ^ i j

j stops at 2 , the first element from right to left that is smaller than 8. (j = 7) if (i < j) swap (a[i], a[j]); else break; i = 2 is less than j=7 and we swap 25 and 2:
5, 3, 2, 6, 10, 17, 1, 25, 18, 8 ^ ^ i j

3.2. Second run of the for loop:


5, 3, 2, 6, 10, 17, 1, 25, 18, 8 ^ ^ i j

We start with i = 2 , pointing to 2, and j = 7, pointing to 25 while (a[++i] < pivot) {} 6 is less then the pivot (8), we skip it, and i stops at 10 ( i = 4)
5, 3, 2, 6, 10, 17, 1, 25, 18, 8 ^ ^ i j

while (pivot < a[--j]) {} 1 is less than the pivot, j stops there. (j = 6)
5, 3, 2, 6, 10, 17, 1, 25, 18, 8 ^ ^ i j

if (i < j) swap (a[i], a[j]); else break; i = 4 is less than j = 6 and we swap 10 and 1:
5, 3, 2, 6, 1, 17, 10, 25, 18, 8 ^ ^ i j

3.3. Third run of the for loop: We start with i = 4 , pointing to 1, and j = 6, pointing to 10.
5, 3, 2, 6, 1, 17, 10, 25, 18, 8 ^ ^ i j

while (a[++i] < pivot {} 17 is greater than the pivot and i stops there (i = 5) while (pivot < a[--j] < {}

2 of 3

18/01/2013 22:47

Quicksort

http://faculty.simpson.edu/lydia.sinapova/www/cmsc250/LN250_Trem...

1 is less then the pivot and j stops there ( j = 4)


5, 3, 2, 6, 1, 17, 10, 25, 18, 8 ^ ^ j i

if (i < j) swap (a[i], a[j]); else break; Now we break, because i and j have crossed 4. Bring back the pivot: The last element is swapped with a[i] 5, 3, 2, 6, 1, 8, 10, 25, 18, 17 This is the first partitioning: [ 5, 3, 2, 6, 1 ] [8] [10, 25, 18, 17] II. Recursively we process the left part of the array: 5, 3, 2, 6, 1 1. Choosing the pivot: the median of three: 5, 2, 1: The median is 2 2. Order the three elements and hide the pivot: 1, 3, 5, 6, 2 3. Start the for loop. a. i will stop at 3 ( i = 1) - the first element greater than the pivot: b. j will stop at 1 (j = 0)- the first element from right to left smaller than the pivot. No swap is done, as i and j have crossed: 4. Bring back the pivot: swap the last element with a[1] 1, 2, 5, 6, 3 The partitioning now is: [ 1 ] [ 2 ] [ 5, 6, 3 ] III. Do the same with the left array and the right array. The left array consists only of one element, so we stop. In practice, we stop partitioning when the elements are small in number, e.g. n < 10, and we sort with insertion sort. Here we stop at 1, for the sake of the example. Next comes the right array: [5, 6, 3] It is only of three elements, and the median of three choice will put them in order: 3, 5, 6 Now we have a sorted part: 1, 2, 3, 5, 6 IV. In a similar way we process the right part of the original array: 10, 25, 18, 17. After it has been sorted, we will obtain: [1, 2, 3, 5, 6] [8] [10, 17, 18, 25] and this is the whole array sorted. Back to QuickSort

3 of 3

18/01/2013 22:47

Das könnte Ihnen auch gefallen