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

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

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

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

View Full Document
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
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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 07/25/2008 for the course CSE 830 taught by Professor Ofria during the Spring '08 term at Michigan State University.

### 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
Ask a homework question - tutors are online