350 likes | 375 Views
Learn about partitioning techniques with the Quicksort algorithm, including selecting a pivot, rearranging elements, and placing the pivot correctly for better sorting efficiency.
E N D
Partitioning • Partitioning can be tricky, here is one method that works well: <p p p 1. Select a pivot, and exchange it with the last element 2. Set i to the first element, and j to the next-to-last element 3. While i < j increment i until an element >= the pivot is found decrement j until an element <= the pivot is found if i < j, swap the elements 4. Exchange the pivot with the element at i CS2 - Quicksort
Partitioning 2 97 17 37 12 46 10 55 80 42 39 Pick pivot == 37 CS2 - Quicksort
Partitioning 2 97 17 39 12 46 10 55 80 42 37 Step 1: Move pivot to end of array CS2 - Quicksort
Partitioning 2 97 17 39 12 46 10 55 80 42 37 Step 1: Move pivot to end of array CS2 - Quicksort
i j Partitioning 2 97 17 39 12 46 10 55 80 42 37 Step 2: set i == 0 and j == array.length - 1 CS2 - Quicksort
i j Partitioning 2 97 17 39 12 46 10 55 80 42 37 Step 3: move i left until value larger than the pivot is found CS2 - Quicksort
i j Partitioning 2 97 17 39 12 46 10 55 80 42 37 Step 4: move j right until value less than the pivot is found CS2 - Quicksort
i j Partitioning 2 10 17 39 12 46 97 55 80 42 37 Step 5: swap elements at positions i and j CS2 - Quicksort
i j Partitioning 2 10 17 39 12 46 97 55 80 42 37 Step 6: move i left until value larger than the pivot is found CS2 - Quicksort
i j Partitioning 2 10 17 39 12 46 97 55 80 42 37 Step 7: move j right until value less than the pivot is found CS2 - Quicksort
i j Partitioning 2 10 17 12 39 46 97 55 80 42 37 Step 8: swap elements at positions i and j CS2 - Quicksort
i j Partitioning 2 10 17 12 39 46 97 55 80 42 37 Step 9: move i left until it hits j CS2 - Quicksort
i j Partitioning 2 10 17 12 37 46 97 55 80 42 39 Step 10: put pivot in correct spot CS2 - Quicksort
pivot Partitioning 2 10 17 12 37 46 97 55 80 42 39 < pivot pivot CS2 - Quicksort
Partitioning public static void partition( int numbers[], int lo, int hi ) { int i = lo + 1, j = hi, pivot = numbers[lo]; while ( i < j ) { while ( numbers[i] < pivot ) i++; while ( numbers[j] > pivot ) j++; if ( i <= j ) { int tmp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = tmp; } } numbers[0] = numbers[i]; numbers[i] = pivot; return i; } CS2 - Quicksort
Quicksort • The quicksort algorithm (S is the array of elements to be sorted): • Selecting the pivot wisely can significantly improve the performance of the algorithm 1. If the length of S is 0 or 1, return 2. Pick any element p in S; this is the pivot 3. Partition S (by rearranging its elements) into two subsets: S1, containing all elements of S-p which are < p S2, containing all elements of S-p which are >= p 4. Return quicksort(S1) followed by p, followed by quicksort(S2) CS2 - Quicksort
Selecting the Pivot • Pivot = First Element • simple, but often the worst possible choice. Consider what happens if the array is already sorted • Pivot = Random Element • safe is almost all situations, however, calculating a random number can be costly • Pivot = Median Value • optimal, but impractical to implement • Pivot = Median of three • a compromise, gives reasonably good performance CS2 - Quicksort
n n/2 n/2 n/4 n/4 n/4 n/4 log2n n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 Analysis n Best Case Complexity nlog2n CS2 - Quicksort
n n … 3 2 1 Analysis n n-1 n-2 n-3 Worst Case Complexity n2 CS2 - Quicksort
Merging • Merging two sorted arrays into a single sorted array is straight forward 1. Set i and j to 0 2. While i < array1.length and j < array2.length 3. If array1[i]<array2[j] then copy array1[I] to the new array and increment i otherwise, copy array2[j] to the new array and increment j 4. Copy the rest of the “non-empty” array to the new array CS2 - Quicksort
i j k Merging 2 3 8 3 4 6 7 CS2 - Quicksort
i j k Merging 2 3 8 3 4 6 7 2 CS2 - Quicksort
i j k Merging 2 3 8 3 4 6 7 2 CS2 - Quicksort
i j k Merging 2 3 8 3 4 6 7 2 3 CS2 - Quicksort
i j k Merging 2 3 8 3 4 6 7 2 3 3 CS2 - Quicksort
i j k Merging 2 3 8 3 4 6 7 2 3 3 4 CS2 - Quicksort
i j k Merging 2 3 8 3 4 6 7 2 3 3 4 6 CS2 - Quicksort
i j k Merging 2 3 8 3 4 6 7 2 3 3 4 6 7 CS2 - Quicksort
i j k Merging 2 3 8 3 4 6 7 2 3 3 4 6 7 8 CS2 - Quicksort
2 97 17 39 12 46 10 55 80 42 37 2 97 17 39 12 46 10 55 80 42 37 2 97 17 39 12 46 10 55 80 42 37 39 12 10 55 42 37 Merge Sort 2 97 17 39 12 46 10 55 80 42 37 CS2 - Quicksort
Merge Sort 2 97 17 39 12 46 10 55 80 42 37 2 97 17 39 12 46 10 55 80 42 37 2 97 17 39 12 46 10 55 80 42 37 2 97 17 12 39 46 10 55 80 37 42 39 12 10 55 42 37 CS2 - Quicksort
Merge Sort 2 97 17 39 12 46 10 55 80 42 37 2 97 17 39 12 46 10 55 80 42 37 2 97 12 17 39 10 46 55 37 42 80 2 97 17 12 39 46 10 55 80 37 42 39 12 10 55 42 37 CS2 - Quicksort
Merge Sort 2 97 17 39 12 46 10 55 80 42 37 2 12 17 39 97 10 37 42 46 55 80 2 97 12 17 39 10 46 55 37 42 80 2 97 17 12 39 46 10 55 80 37 42 39 12 10 55 42 37 CS2 - Quicksort
Merge Sort 2 10 12 17 37 39 42 46 55 80 97 2 12 17 39 97 10 37 42 46 55 80 2 97 12 17 39 10 46 55 37 42 80 2 97 17 12 39 46 10 55 80 37 42 39 12 10 55 42 37 CS2 - Quicksort
n n/2 n/2 n/4 n/4 n/4 n/4 log2n n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 Analysis n Complexity nlog2n CS2 - Quicksort