1 / 19

Quicksort: A More Efficient Exchange Sorting Scheme than Bubble Sort

Quicksort is a divide-and-conquer sorting algorithm that uses a recursive approach to efficiently sort elements. It divides the original problem into simpler sub-problems and independently solves each sub-problem until they are simple enough to be directly solved. Quicksort uses a pivot to partition the elements and performs exchanges to correctly position the elements. It has a best-case time complexity of O(nlogn) and a worst-case time complexity of O(n^2).

rutledged
Download Presentation

Quicksort: A More Efficient Exchange Sorting Scheme than Bubble 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. Quicksort Dr. Yingwu Zhu

  2. Quicksort • A more efficient exchange sorting scheme than bubble sort • A typical exchange involves elements that are far apart • Fewer interchanges are required to correctly position an element. • Quicksort uses a divide-and-conquer strategy • A recursive approach • The original problem partitioned into simpler sub-problems, • Each sub problem considered (conquered) independently. • Subdivision continues until sub problems obtained are simple enough to be solved directly • How simple?

  3. Quicksort: Divide/Split • Choose some element called a pivot • Perform a sequence of exchanges so that • All elements that are less than this pivot are to its left. • All elements that are greater than the pivot are to its right. • Divides the (sub)list into two smaller sub lists, • Each of which may then be sorted independently in the same way.

  4. Quicksort If the list has 0 or 1 elements, return. // the list is sorted, simple enough! Else do: Pick an element in the list to use as the pivot.   Split the remaining elements into two disjoint groups: SmallerThanPivot = {all elements <= pivot} LargerThanPivot = {all elements > pivot}  Return the list rearranged as: Quicksort(SmallerThanPivot), pivot, Quicksort(LargerThanPivot). Split

  5. Quicksort Example • Given to sort:75, 70, 65, , 98, 78, 100, 93, 55, 61, 81, • Select, arbitrarily, the first element, 75, as pivot. • Search from right for elements <= 75, stop at first element <=75 • Search from left for elements > 75, stop at first element >75 • Swap these two elements, and then repeat this process 68 84

  6. Quicksort Example 75, 70, 65, 68, 61, 55, 100, 93, 78, 98, 81, 84 • When done, swap with pivot • This SPLIT operation placed pivot 75 so that all elements to the left were <= 75 and all elements to the right were > 75. • 75 is now placed appropriately • Need to sort sublists on either side of 75

  7. Quicksort Example • Need to sort (independently): 55, 70, 65, 68, 61 and 100, 93, 78, 98, 81, 84 • Let pivot be 55, look from each end for values larger/smaller than 55, swap • Same for 2nd list, pivot is 100 • Sort the resulting sublists in the same manner until sublist is trivial (size 0 or 1)

  8. Quicksort • Note visual example ofa quicksort on an array etc. …

  9. Reflection of Quicksort • Perform spilt() operation on a (sub)list, such that:left-sublist, pivot, right-sublist • Recursively and independently perform split() on left-sublist and right-sublist, until their sizes become 0 or 1 (simple enough). • So, the basic operation is split! • int split(int x[], int first, int last) • [first, last] specifies the sublist. • Returns the final position of the pivot

  10. Implementing Quicksort • Basic operation: split • Choose the pivot (e.g., the first element) • Scan the (sub)list from both ends, swap elements such that the resulting left sublist < pivot and right sublist >= pivot • int split (int x[], int first, int last)

  11. int split(int x[], int first, int last) { int pivot = x[first]; int left = first, right = last; while(left < right) { while(left < right && pivot < x[right]) right--; while(left < right && x[left] <= pivot) left++; if (left < right) swap(x[left], x[right]); } //end while x[first] = x[right]; x[right] = pivot; return right; }

  12. Recursive Quicksort • void quicksort(int x[], int n)

  13. void quicksort(int x[], int n) { quicksort_aux(x,0,n-1); } void quicksort_aux(int x[], int first, int last) { if (first < last) { int pos = split(x, first, last); quicksort_aux(x, first, pos-1); quicksort_aux(x, pos+1, last); } }

  14. Quicksort: T(n) • Best-case ? • Worst-case ?

  15. Quicksort Performance • O(nlog2n) is the best case computing time • If the pivot results in sublists of approximately the same size. • O(n2) worst-case • List already ordered, elements in reverse • When Split() repetitively results, for example, in one empty sublist

  16. Improvements to Quicksort • An arbitrary pivot gives a poor partition for nearly sorted lists (or lists in reverse) • Virtually all the elements go into either SmallerThanPivotor LargerThanPivot • all through the recursive calls. • Quicksort takes quadratic time to do essentially nothing at all.

  17. Improvements to Quicksort • Better method for selecting the pivot is the median-of-three rule, • Select the median of the first, middle, and last elements in each sublist as the pivot. • Often the list to be sorted is already partially ordered • Median-of-three rule will select a pivot closer to the middle of the sublist than will the “first-element” rule.

  18. Improvements to Quicksort • For small files (n <= 20), quicksort is worse than insertion sort; • small files occur often because of recursion. • Use an efficient sort (e.g., insertion sort) for small files. • Better yet, use Quicksort() until sublists are of a small size and then apply an efficient sort like insertion sort.

  19. Non-recursive Quicksort • Think about non-recursive alg.?

More Related