QuickSort-new-2

# QuickSort-new-2 - Quick-Sort 3:39 PM Quick-Sort 7 4 9 6 2 2...

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

Quick-Sort 2/1/2006 3:39 PM 1 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 (§4.3) ± Algorithm ± Partition step ± Quick-sort tree ± Execution example Analysis of quick-sort (4.3.1) In-place quick-sort (§4.8) Summary of sorting algorithms

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

View Full Document
Quick-Sort 2/1/2006 3:39 PM 2 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 2/1/2006 3:39 PM 3 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 ² Sorted sequence at the end of the execution ± The root is the initial call ± The leaves are calls on subsequences of size 0 or 1 7 4 9 6 2 2 4 6 7 9 4 2 2 4 7

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.

{[ snackBarMessage ]}

### Page1 / 10

QuickSort-new-2 - Quick-Sort 3:39 PM Quick-Sort 7 4 9 6 2 2...

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

View Full Document
Ask a homework question - tutors are online