1 / 4

Quick Sort

Quick Sort. High level version Partition AlmostQuickSort. QuickSort. QuickSort(l,r) If (r-l) < 1 Return i  Partition(l,r) QuickSort(l,i-1) QuickSort(i+1,r) Partition(l,r). r. l. i. Partition. How can you Partition, so that: Partition is O(n)

fia
Download Presentation

Quick Sort

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. Quick Sort High level version Partition AlmostQuickSort CS 303 – Quick Sort Lecture 16

  2. QuickSort • QuickSort(l,r) • If (r-l) < 1 Return • i  Partition(l,r) • QuickSort(l,i-1) • QuickSort(i+1,r) • Partition(l,r) r l i CS 303 – Quick Sort Lecture 16

  3. Partition • How can you Partition, so that: • Partition is O(n) • Why? • If both these conditions are met, then QuickSort is O(n log n) • If not...not! • Discuss various Partition techniques • scan from l,r inwards – swap when stuck • scan from l  r – swap when <x • Discuss choice of pivot • use statistical sample to improve odds CS 303 – Quick Sort Lecture 16

  4. AlmostQuickSort • AlmostQuickSort(l,r) • If (r-l) < 15 Return • i  Partition(l,r) • AlmostQuickSort(l,i-1) • AlmostQuickSort(i+1,r) • QuickSort(l,r) • AlmostQuickSort(l,r) • InsertionSort(l,r) • Partition becomes easier to code • Final InsertionSort is O(n) • alas...(Almost)QuickSort is still O(n2) (in theory...but...) CS 303 – Quick Sort Lecture 16

More Related