{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

QuickSort

# QuickSort - Quick-Sort 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.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern