DOC PREVIEW
ASU CSE 310 - L06

This preview shows page 1-2-3-4 out of 13 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSE310 Lecture 06: Quick-SortTopics of this lectureQuick SortExample: Sort people by their agesQuick Sort AlgorithmHow is the partition module implemented?Algorithm for PartitioningExampleComplexity of Quick-SortAverage Complexity of Quick-SortStable Sorting and Non-Stable SortingSummaryReadings and [email protected] Lecture 06:CSE310 Lecture 06:Quick-SortQuick-SortGuoliang (Larry) XueDepartment of CSEArizona State Universityhttp://optimization.asu.edu/[email protected] of this lectureTopics of this lectureDivide and ConquerDivideConquerCombineQuick SortDescriptionExampleWorst-case time complexityAverage-case time [email protected]Divide: Divide the sequence into 2 subsequences, such that each element in the 1st subsequence is less than each element in the 2nd subsequence;Conquer: Sort each subsequence recursively using quick sortCombine: no work is needed, because it sorts in place.Quick SortQuick [email protected]: Sort people by their agesExample: Sort people by their agesage25peopleage ≤ 25peopleage > 25age23peopleage ≤ 23age30peopleage >30peopleage >23peopleage ≤ [email protected] (A, p,r) // different from the textif p < r thenk := Partition(A, p, r)Quicksort (A, p, k-1)Quicksort (A, k+1, r)The crux of the algorithm is the partition module, it rearranges the list so that:•the element A[k] (pivot) is in its final place in the list•all the elements in A[p].. A[k-1] are less than or equal to A[k].•all the elements in A[k+1] .. A[r] are greater than A[k] ApkrQuick Sort AlgorithmQuick Sort [email protected], we can rearrange the list, so that half elements go to left and half elements go to right of i. In practice:1. Choose A[r] as the pivot element to go to the correct place in the list.2. If p ≤k≤i, then A[k]≤A[r];3. If i+1 ≤ k ≤ j-1, then A[k] > A[r];4. Scan from left to right, and do O(1) work at each position.How is the How is the partit ionpartit ion module implemented? module [email protected](A, p, r)1 x := A[r]2 i := p-13 for j:=p to r-1 do4 If A[j] ≤ x then5 i := i+16 exchange A[i] and A[j]7exchange A[i+1] and A[r]8return i+1Algorithm for PartitioningAlgorithm for [email protected] the example on page 172 of the [email protected] partitioning: if the list is already sorted:The right sublist: 0, and left sublist: n - 1 elements)()1()2()()1()( nnnTnnTnT )(2)1()()(211nnnkknknkThe complexity depends on the partition.Best-case partitioning: divide into 2 equal sublistsotherwisennTnifnT)()2/(21)1()(T(n) = (nlgn)Complexity of Quick-SortComplexity of [email protected] Complexity of Quick-SortAverage Complexity of Quick-SortPartition into (q-1)-to-(n-q) split. All permutations are equally likely.q = 1, …, nT(n)=n+1+2(T(0)+T(1)+…+T(n-1))/nnT(n)=n(n+1)+2(T(0)+T(1)+…+T(n-1))(n-1)T(n-1)=(n-1)n+2(T(0)+T(1)+…+T(n-2))nT(n)-(n-1)T(n-1)=2n+2T(n-1)nT(n)=2n+(n+1)T(n-1)T(n)/(n+1)=2/(n+1)+T(n-1)/nT(n)=(n+1)2(1+1/2+1/3+…+1/(n+1)) = (n log n)[email protected] Sorting and Non-Stable SortingStable Sorting and Non-Stable SortingA sorting algorithm is stable if A[i]=A[j] implies the relative order of the two elements will not change during the sorting process.Insertion sort is stable.Quick sort is not [email protected]Algorithm Design: Divide and ConquerAlgorithm: QuicksortAnalysis: Average-case [email protected] and ExercisesReadings and ExercisesThe materials covered in this lecture can be found in Sections 7.1-7.4.Exercise 7.4-1 and 7.4-2.Study the Hoare partition on page 185.Lecture 07 will be on


View Full Document

ASU CSE 310 - L06

Documents in this Course
Load more
Download L06
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view L06 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view L06 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?