Quicksort
description
Transcript of Quicksort
![Page 1: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/1.jpg)
Quicksort
David Kauchak
cs062
Spring 2010
![Page 2: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/2.jpg)
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1;
for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } }
swap(nums, lessThanIndex+1, end);
return lessThanIndex+1;}
what does this method do?
![Page 3: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/3.jpg)
nums[end] is called the pivot Partitions the elements A[start…end-1] in to two sets, those ≤
pivot and those > pivot Operates in place Final result:
nums
start pivot end
≤ pivot > pivot
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
![Page 4: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/4.jpg)
… 5 7 1 2 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
![Page 5: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/5.jpg)
lessThanIndex
… 5 7 1 2 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
![Page 6: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/6.jpg)
lessThanIndex
… 5 7 1 2 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 7: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/7.jpg)
lessThanIndex
… 5 7 1 2 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 8: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/8.jpg)
lessThanIndex
… 5 7 1 2 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 9: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/9.jpg)
lessThanIndex
… 5 7 1 2 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 10: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/10.jpg)
lessThanIndex
… 5 7 1 2 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 11: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/11.jpg)
lessThanIndex
… 5 7 1 2 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 12: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/12.jpg)
lessThanIndex
… 5 7 1 2 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 13: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/13.jpg)
lessThanIndex
… 5 1 7 2 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 14: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/14.jpg)
lessThanIndex
… 5 1 7 2 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 15: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/15.jpg)
lessThanIndex
… 5 1 7 2 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 16: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/16.jpg)
lessThanIndex
… 5 1 2 7 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 17: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/17.jpg)
lessThanIndex
… 5 1 2 7 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 18: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/18.jpg)
lessThanIndex
… 5 1 2 7 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 19: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/19.jpg)
lessThanIndex
… 5 1 2 7 8 4 3 6 …
start end
i
What’s happening?
![Page 20: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/20.jpg)
lessThanIndex
… 5 1 2 7 8 4 3 6 …
start end
i
≤ pivot > pivot unprocessed
![Page 21: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/21.jpg)
lessThanIndex
… 5 1 2 7 8 4 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 22: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/22.jpg)
lessThanIndex
… 5 1 2 4 8 7 3 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 23: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/23.jpg)
lessThanIndex
… 5 1 2 4 3 7 8 6 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 24: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/24.jpg)
lessThanIndex
… 5 1 2 4 3 6 8 7 …
start end
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
i
![Page 25: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/25.jpg)
Partition running time?
O(n)
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
![Page 26: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/26.jpg)
Quicksort
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
How can we use this method to sort nums?
![Page 27: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/27.jpg)
Quicksort
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
public static int partition(double[] nums, int start, int end){ int lessThanIndex = start-1; for( int i = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1;}
![Page 28: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/28.jpg)
8 5 1 3 6 2 7 4
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
![Page 29: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/29.jpg)
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
8 5 1 3 6 2 7 4
![Page 30: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/30.jpg)
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
1 3 2 4 6 8 7 5
![Page 31: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/31.jpg)
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
1 3 2 4 6 8 7 5
![Page 32: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/32.jpg)
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
1 3 2 4 6 8 7 5
![Page 33: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/33.jpg)
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
1 2 3 4 6 8 7 5
![Page 34: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/34.jpg)
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
1 2 3 4 6 8 7 5
![Page 35: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/35.jpg)
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
1 2 3 4 6 8 7 5
![Page 36: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/36.jpg)
1 2 3 4 6 8 7 5
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
![Page 37: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/37.jpg)
1 2 3 4 6 8 7 5
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
![Page 38: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/38.jpg)
1 2 3 4 5 8 7 6
What happens here?
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
![Page 39: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/39.jpg)
1 2 3 4 5 8 7 6
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
![Page 40: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/40.jpg)
1 2 3 4 5 8 7 6
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
![Page 41: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/41.jpg)
1 2 3 4 5 6 7 8
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
![Page 42: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/42.jpg)
1 2 3 4 5 6 7 8
private static void quicksortHelper(double[] nums, int start, int end){ if( start < end ){ int partition = partition(nums, start, end); quicksortHelper(nums, start, partition-1); quicksortHelper(nums, partition+1, end); }}
![Page 43: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/43.jpg)
Running time of Quicksort?
Worst case? Each call to Partition splits the array into an empty
array and n-1 array
![Page 44: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/44.jpg)
Quicksort: Worst case running time
When does this happen? sorted reverse sorted near sorted/reverse sorted
n-1 + n-2 + n-3 + … + 1 = O(n2)
![Page 45: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/45.jpg)
Quicksort best case?
Each call to Partition splits the array into two equal parts
…
How much work is done at each “level”, i.e. running time of a level?
O(n)
![Page 46: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/46.jpg)
Quicksort best case?
Each call to Partition splits the array into two equal parts
…How many levels are there?
Similar to binary search, each call to Parition will throw away half the data until we’re down to one element: log2 n levels
![Page 47: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/47.jpg)
Quicksort best case?
Each call to Partition splits the array into two equal parts
…Overall runtime?
O(n log n)
![Page 48: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/48.jpg)
Quicksort Average case? Two intuitions
As long as the Partition procedure always splits the array into some constant ratio between the left and the right, say L-to-R, e.g. 9-to-1, then we maintain O(n log n)
As long as we only have a constant number of “bad” partitions intermixed with a “good partition” then we maintain O(n log n)
![Page 49: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/49.jpg)
How can we avoid the worst case?
Inject randomness into the data
private static void randomizedPartition(double[] nums, int start, int end){ int i = random(start, end); swap(nums, i, end); return partition = partition(nums, start, end);}
Randomized quicksort is average case O(n log n)
![Page 50: Quicksort](https://reader033.fdocuments.in/reader033/viewer/2022051516/568144f7550346895db1c3c1/html5/thumbnails/50.jpg)
What is the wost case running time of randomized Quicksort?
O(n2)
We could still get very unlucky and pick “bad” partitions at every step