1 / 50

Quicksort

Quicksort. David Kauchak cs062 Spring 2011. 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 ); }

owen
Download Presentation

Quicksort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Quicksort David Kauchak cs062 Spring 2011

  2. public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = 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?

  3. public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; } • nums[end] is called the pivot • Partitions the elements A[start…end-1] into two sets, those ≤ pivot and those > pivot • Operates in place • Final result: end start pivot nums > pivot ≤ pivot

  4. start end … 5 7 1 2 8 4 3 6 … public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  5. lessThanIndex … 5 7 1 2 8 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  6. i lessThanIndex … 5 7 1 2 8 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  7. i lessThanIndex … 5 7 1 2 8 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  8. i lessThanIndex … 5 7 1 2 8 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  9. i lessThanIndex … 5 7 1 2 8 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  10. i lessThanIndex … 5 7 1 2 8 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  11. i lessThanIndex … 5 7 1 2 8 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  12. i lessThanIndex … 5 7 1 2 8 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  13. i lessThanIndex … 5 1 72 8 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  14. i lessThanIndex … 5 1 7 2 8 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  15. i lessThanIndex … 5 1 7 2 8 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  16. i lessThanIndex … 5 1 2 78 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  17. i lessThanIndex … 5 1 2 78 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  18. i lessThanIndex … 5 1 2 78 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  19. i lessThanIndex … 5 1 2 78 4 3 6 … start end What’s happening?

  20. i lessThanIndex … 5 1 2 78 4 3 6 … start end ≤ pivot > pivot unprocessed

  21. i lessThanIndex … 5 1 2 78 4 3 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  22. i lessThanIndex … 5 1 2 48 73 6 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  23. i lessThanIndex … 5 1 2 4 3 7 86 … start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  24. i lessThanIndex … 5 1 2 4 3 6 8 7… start end public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  25. Partition running time? • O(n) public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  26. Quicksort How can we use this method to sort nums? public static intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  27. Quicksort private static voidquicksortHelper(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 intpartition(double[] nums, int start, int end){ intlessThanIndex = start-1; for( inti = start; i < end; i++ ){ if( nums[i] <= nums[end] ){ lessThanIndex++; swap(nums, lessThanIndex, i); } } swap(nums, lessThanIndex+1, end); return lessThanIndex+1; }

  28. 8 5 1 3 6 2 7 4 private static voidquicksortHelper(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); } }

  29. 8 5 1 3 6 2 7 4 private static voidquicksortHelper(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); } }

  30. 1 3 2 4 6 8 7 5 private static voidquicksortHelper(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); } }

  31. 1 3 2 4 6 8 7 5 private static voidquicksortHelper(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); } }

  32. 1 3 2 4 6 8 7 5 private static voidquicksortHelper(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); } }

  33. 1 2 3 4 6 8 7 5 private static voidquicksortHelper(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); } }

  34. 1 2 3 4 6 8 7 5 private static voidquicksortHelper(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); } }

  35. 1 2 3 4 6 8 7 5 private static voidquicksortHelper(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); } }

  36. 1 2 3 4 6 8 7 5 private static voidquicksortHelper(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); } }

  37. 1 2 3 4 6 8 7 5 private static voidquicksortHelper(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); } }

  38. 1 2 3 4 5 8 7 6 What happens here? private static voidquicksortHelper(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); } }

  39. 1 2 3 4 5 8 7 6 private static voidquicksortHelper(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); } }

  40. 1 2 3 4 5 8 7 6 private static voidquicksortHelper(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); } }

  41. 1 2 3 4 5 6 7 8 private static voidquicksortHelper(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); } }

  42. 1 2 3 4 5 6 7 8 private static voidquicksortHelper(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); } }

  43. Running time of Quicksort? • Worst case? • Each call to Partition splits the array into an empty array and n-1 array

  44. Quicksort: Worst caserunning time • When does this happen? • sorted • reverse sorted • near sorted/reverse sorted n-1 + n-2 + n-3 + … + 1 = O(n2)

  45. 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)

  46. Quicksort best case? • Each call to Partition splits the array into two equal parts … How many levels are there? Similar to merge sort: log2n levels

  47. Quicksort best case? • Each call to Partition splits the array into two equal parts … Overall runtime? O(n log n)

  48. 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)

  49. How can we avoid the worst case? • Inject randomness into the data private static voidrandomizedPartition(double[] nums, int start, int end){ inti = random(start, end); swap(nums, i, end); return partition = partition(nums, start, end); } Randomized quicksort is average case O(n log n)

  50. 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

More Related