Lecture05 - CSE 830: Design and Theory of Algorithms...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSE 830: Design and Theory of Algorithms Quicksort! Dr. Charles Ofria Quicksort Although mergesort is O( n log n ), it is difficult to implement on arrays since we need space to merge. In practice, Quicksort is the fastest sorting algorithm. Example: Pivot about 10 17 12 6 23 19 8 5 10 - before 6 8 5 10 17 12 23 19 - after The pivot point is now in the correctly sorted position, and all other numbers are in the relative correct position, before or after. Quicksort Walkthrough 17 12 6 23 19 8 5 10 6 8 5 10 17 12 23 19 5 6 8 17 12 19 23 6 8 12 17 23 6 17 5 6 8 10 12 17 19 23
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Pseudocode Sort(A) { Quicksort(A,1,n); } Quicksort(A, low, high) { if (low < high) { pivotLocation = Partition(A,low,high); Quicksort(A,low, pivotLocation - 1); Quicksort(A, pivotLocation+1, high); } } Pseudocode Partition(A,low,high) { pivot = A[low]; leftwall = low; for i = low+1 to high { if (A[i] < pivot) then { leftwall = leftwall+1; swap(A[i],A[leftwall]); } } swap(A[low],A[leftwall]); return leftwall; } Best Case for Quicksort
Background image of page 2
3 Worst Case for Quicksort Intuition: The Average Case 0 n /4 n /2 3 n /4 n Anywhere in the middle half is a decent partition (3/4) h n = 1 => n = (4/3) h log( n ) = h log(4/3) h = log( n ) / log(4/3) < 2 log( n ) What have we shown? At most 2log( n ) decent partitions suffices to sort an array of n elements. But if we just take arbitrary pivot points, how often will they, in fact, be decent? Since any number ranked between
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

Lecture05 - CSE 830: Design and Theory of Algorithms...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online