Partitioning p p p Partitioning can be tricky
Partitioning <p p p • Partitioning can be tricky, here is one method that works well: 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 9/18/2020 CS 2 - Quicksort 1
Partitioning 2 97 17 37 12 46 10 55 80 42 39 Pick pivot == 37 9/18/2020 CS 2 - Quicksort 2
Partitioning 2 97 17 39 12 46 10 55 80 42 37 Step 1: Move pivot to end of array 9/18/2020 CS 2 - Quicksort 3
Partitioning 2 97 17 39 12 46 10 55 80 42 37 Step 1: Move pivot to end of array 9/18/2020 CS 2 - Quicksort 4
Partitioning i 2 j 97 17 39 12 46 10 55 80 42 37 Step 2: set i == 0 and j == array. length - 1 9/18/2020 CS 2 - Quicksort 5
Partitioning i 2 97 17 39 j 12 46 10 55 80 42 37 Step 3: move i left until value larger than the pivot is found 9/18/2020 CS 2 - Quicksort 6
Partitioning i 2 97 17 39 j 12 46 10 55 80 42 37 Step 4: move j right until value less than the pivot is found 9/18/2020 CS 2 - Quicksort 7
Partitioning i 2 10 17 39 j 12 46 97 55 80 42 37 Step 5: swap elements at positions i and j 9/18/2020 CS 2 - Quicksort 8
Partitioning i 2 10 17 39 j 12 46 97 55 80 42 37 Step 6: move i left until value larger than the pivot is found 9/18/2020 CS 2 - Quicksort 9
Partitioning i 2 10 17 39 j 12 46 97 55 80 42 37 Step 7: move j right until value less than the pivot is found 9/18/2020 CS 2 - Quicksort 10
Partitioning i 2 10 17 12 j 39 46 97 55 80 42 37 Step 8: swap elements at positions i and j 9/18/2020 CS 2 - Quicksort 11
Partitioning i j 2 10 17 12 39 46 97 55 80 42 37 Step 9: move i left until it hits j 9/18/2020 CS 2 - Quicksort 12
Partitioning i j 2 10 17 12 37 46 97 55 80 42 39 Step 10: put pivot in correct spot 9/18/2020 CS 2 - Quicksort 13
Partitioning pivot 2 10 17 12 37 46 97 55 80 42 39 pivot < pivot 9/18/2020 CS 2 - Quicksort 14
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; } 9/18/2020 CS 2 - Quicksort 15
Quicksort • The quicksort algorithm (S is the array of elements to be sorted): 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: S 1, containing all elements of S-p which are < p S 2, containing all elements of S-p which are >= p 4. Return quicksort(S 1) followed by p, followed by quicksort(S 2) • Selecting the pivot wisely can significantly improve the performance of the algorithm 9/18/2020 CS 2 - Quicksort 16
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 9/18/2020 CS 2 - Quicksort 17
Analysis n n n/2 n/4 n/8 n/8 log 2 n n/8 Best Case Complexity nlog 2 n 9/18/2020 CS 2 - Quicksort 18
Analysis n n n-1 n-2 n-3 n … 3 2 1 Worst Case Complexity n 2 9/18/2020 CS 2 - Quicksort 19
Merging • Merging two sorted arrays into a single sorted array is straight forward 1. Set i and j to 0 2. While i < array 1. length and j < array 2. length 3. If array 1[i]<array 2[j] then copy array 1[I] to the new array and increment i otherwise, copy array 2[j] to the new array and increment j 4. Copy the rest of the “non-empty” array to the new array 9/18/2020 CS 2 - Quicksort 20
Merging i 2 j 3 8 3 4 6 7 k 9/18/2020 CS 2 - Quicksort 21
Merging i 2 3 j 8 3 4 6 7 2 k 9/18/2020 CS 2 - Quicksort 22
Merging i 2 3 j 8 3 4 6 7 2 k 9/18/2020 CS 2 - Quicksort 23
Merging i 2 3 j 8 3 4 6 7 k 9/18/2020 CS 2 - Quicksort 24
Merging i 2 3 8 2 3 3 j 3 4 6 7 k 9/18/2020 CS 2 - Quicksort 25
Merging i 2 3 8 2 3 3 j 3 4 6 7 4 k 9/18/2020 CS 2 - Quicksort 26
Merging i 2 3 8 2 3 3 j 3 4 4 6 7 6 k 9/18/2020 CS 2 - Quicksort 27
Merging i 2 3 8 2 3 3 j 3 4 6 7 7 k 9/18/2020 CS 2 - Quicksort 28
Merging i 2 3 8 2 3 3 4 j 6 3 4 7 8 6 7 k 9/18/2020 CS 2 - Quicksort 29
Merge Sort 2 2 97 17 39 97 97 12 17 39 39 9/18/2020 12 46 10 55 80 42 37 12 46 80 12 CS 2 - Quicksort 10 55 42 42 37 37 30
Merge Sort 2 2 97 17 39 97 97 12 17 39 17 46 10 55 80 42 37 12 12 39 39 9/18/2020 12 46 10 55 80 42 37 46 80 12 CS 2 - Quicksort 10 55 37 42 42 37 31
Merge Sort 2 2 97 17 39 97 97 12 12 17 17 46 10 55 80 42 37 39 12 39 39 9/18/2020 12 46 10 55 80 42 37 10 46 46 12 CS 2 - Quicksort 55 37 42 10 55 80 37 42 80 42 37 32
Merge Sort 2 2 97 17 39 2 2 12 17 39 97 97 97 12 17 17 10 37 42 46 55 80 39 12 39 39 9/18/2020 12 46 10 55 80 42 37 10 46 46 12 CS 2 - Quicksort 55 37 42 10 55 80 37 42 80 42 37 33
Merge Sort 2 2 10 12 17 2 2 12 17 39 97 97 97 12 17 17 10 37 42 46 55 80 39 12 39 39 9/18/2020 37 39 42 46 55 80 97 10 46 46 12 CS 2 - Quicksort 55 37 42 10 55 80 37 42 80 42 37 34
Analysis n n n/2 n/4 n/8 n/8 log 2 n n/8 Complexity nlog 2 n 9/18/2020 CS 2 - Quicksort 35
- Slides: 35