# QuickSort - Quick-Sort 4/1/2003 8:53 AM Outline and Reading...

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

Quick-Sort 1 Quick-Sort 7 4 9 6 2 2 4 6 7 9 4 2 2 4 7 9 7 9 2 2 9 9 Quick-Sort 2 Outline and Reading Quick-sort (§10.3) ± Algorithm ± Partition step ± Quick-sort tree ± Execution example Analysis of quick-sort (§10.3.1) In-place quick-sort (§10.3.1) Summary of sorting algorithms Quick-Sort 3 Quick-Sort Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm: ± Divide : pick a random element x (called pivot ) and partition S into ² L elements less than x ² E elements equal x ² G elements greater than x ± Recur : sort L and G ± Conquer : join L , E and G x x L G E x Quick-Sort 4 Partition We partition an input sequence as follows: ± We remove, in turn, each element y from S and ± We insert y into L , E or G , depending on the result of the comparison with the pivot x Each insertion and removal is at the beginning or at the end of a sequence, and hence takes O (1) time Thus, the partition step of quick-sort takes O ( n ) time Algorithm partition ( S, p ) Input sequence S , position p of pivot Output subsequences L, E, G of the elements of S less than, equal to, or greater than the pivot, resp. L, E, G empty sequences x S.remove ( p ) while ¬ S.isEmpty () y S.remove ( S.first ()) if y < x L.insertLast ( y ) else if y = x E.insertLast ( y ) else { y > x } G.insertLast ( y ) return L, E, G Quick-Sort 5 Quick-Sort Tree An execution of quick-sort is depicted by a binary tree ± Each node represents a recursive call of quick-sort and stores ² Unsorted sequence before the execution and its pivot ²

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 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

### Page1 / 3

QuickSort - Quick-Sort 4/1/2003 8:53 AM Outline and Reading...

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

View Full Document
Ask a homework question - tutors are online