notes59 - QuickSort -Popular sorting algorithm that also...

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

View Full Document Right Arrow Icon
QuickSort -Popular sorting algorithm that also uses divide and conquer -It can be implemented “in place” so no extra memory is needed -Worstcase = 0(n^2) -Expected case = 0(nlogn) Algorithm QuickSort(s) Input: Unsorted Array S Output: Sorted Array S Three Steps 1. Divide: If S has only one element, return Otherwise: 1.1. Choose an element x in S that will be called the pivot 1.2. Divide into three sets, L, E, G such that all elements in L are less than x, all elements in G are greater than x, and all elements in E are equal to x. 2. Recur: Call QuickSort(L) and QuickSort(G) to sort branches 3. Conquer: Put elements L, E, G back into S Example Implementation: In place QuickSort -We use the same array S for storage of L, E, G at each step. -We use sub ranges to represent the set. -The last element of the sub range is used as a pivot. -To separate S into L, E, G we will have a “left” and “right” index that points to the beginning and end of the array S. S = ( 85, 24, 63, 45, 17, 31, 96, 50 ) Pivot = 50. L* =85 R* = 96 ---> <--- find element > 50 find element < 50 When we find the elements matching the above conditions we swap them and continue until until L and R meet (L == R) At this point, we swap x with element at R. This will contain L, E, G.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
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 / 6

notes59 - QuickSort -Popular sorting algorithm that also...

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

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