Eric-Wed - CSE 3101: Introduction to the Design and...

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

View Full Document Right Arrow Icon
1 02/14/12 CSE 3101 1 Suprakash Datta datta[at]cse.yorku.ca CSE 3101: Introduction to the Design and  Analysis of Algorithms
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Quick Sort Characteristics sorts ”almost” in place, i.e., does not require an additional array, like insertion sort Divide-and-conquer, like merge sort very practical, average sort performance O(n log n) (with small constant factors), but worst case O(n 2 ) [ CAVEAT: this is true for the CLRS version ]
Background image of page 2
3 Quick Sort – the main idea To understand quick-sort, let’s look at a high- level description of the algorithm A divide-and-conquer algorithm Divide: partition array into 2 subarrays such that elements in the lower part <= elements in the higher part Conquer: recursively sort the 2 subarrays Combine: trivial since sorting is done in place
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Partitioning Linear time partitioning procedure Partition (A,p,r) 01 x A[r] 02 i p-1 03 j r+1 04 while TRUE 05 repeat j j-1 06 until A[j] x 07 repeat i i+1 08 until A[i] x 09 if i<j 10 then exchange A[i] A[j] 11 else return j 17 12 6 19 23 8 5 10 i j 10 12 6 19 23 8 5 17 j i 10 5 6 19 23 8 12 17 j i i j 10 5 6 8 23 19 12 17 X=10
Background image of page 4
5 Quick Sort Algorithm Initial call Quicksort(A, 1, length[A]) Quicksort(A,p,r) 01 if p<r 02 then q Partition(A,p,r) 03 Quicksort(A,p,q) 04 Quicksort(A,q+1,r)
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Analysis of Quicksort Assume that all input elements are distinct The running time depends on the distribution of splits
Background image of page 6
7 Best Case If we are lucky, Partition splits the array evenly ( ) 2 ( / 2) ( ) T n T n n = + Θ
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Using the median as a pivot
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/13/2012 for the course CSE 3101 taught by Professor Andy during the Winter '10 term at York University.

Page1 / 28

Eric-Wed - CSE 3101: Introduction to the Design and...

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

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