UD CISC 320 - Lecture 4 QuickSort & MergeSort (10 pages)

Previewing pages 1, 2, 3 of 10 page document View the full content.
View Full Document

Lecture 4 QuickSort & MergeSort



Previewing pages 1, 2, 3 of actual document.

View the full content.
View Full Document
View Full Document

Lecture 4 QuickSort & MergeSort

40 views


Pages:
10
School:
University of Delaware
Course:
Cisc 320 - Introduction to Algorithms

Unformatted text preview:

CISC 320 Introduction to Algorithms Fall 2005 Lecture 4 QuickSort MergeSort CISC320 F05 Lec4 Liao 1 Terminologies Comparison based sorting In place Stable sorting CISC320 F05 Lec4 Liao 2 1 Copyright The McGraw Hill Companies Inc Permission required for reproduction or display CISC320 F05 Lec4 Liao 3 Copyright The McGraw Hill Companies Inc Permission required for reproduction or display CISC320 F05 Lec4 Liao 4 2 Insertion Sort complexity analysis Worst case Twc n i 1to n 1 i n n 1 2 for loop to see whether A i needs to move while loop to move A i at most i times to the front of the array CISC320 F05 Lec4 Liao 5 Why we save Because elements in range p q 1 and range q 1 r will never be compared in this divide and conquer algorithm CISC320 F05 Lec4 Liao 6 3 Given any pivot element A r it takes n 1 comparisons to partition array A into two subarrays and the right location for A r such that all smaller keys are to its left and larger keys to the right The partition as shown below is in place Is it also stable Copyright The McGraw Hill Companies Inc Permission required for reproduction or display CISC320 F05 Lec4 Liao 7 Copyright The McGraw Hill Companies Inc Permission required for reproduction or display CISC320 F05 Lec4 Liao 8 4 Copyright The McGraw Hill Companies Inc Permission required for reproduction or display 9 CISC320 F05 Lec4 Liao Quicksort ideal case Each call to the partition subroutine will return a splitPoint which is right at the middle of the range namely divide the range into two equal subranges In doing so at most n comparisons are needed to ensure one subrange contains only keys that are smaller than the pivot and the other subrange only keys larger than the pivot Therefore T n 2T n 2 n n n 2 n 4 n n 2 n 4 n 4 n 4 n lg n n lg n CISC320 F05 Lec4 Liao 10 5 Quicksort worst case Each call to the partition subroutine will return a splitPoint which is just the left boundary of the range namely no keys are smaller than the pivot How many comparisons needed to



View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Lecture 4 QuickSort & MergeSort 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 Lecture 4 QuickSort & MergeSort 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?