efficient algorithms quicksort. quicksort a common algorithm for sorting non-sorted arrays....

11
Efficient Algorithms Quicksort

Upload: deirdre-francis

Post on 11-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the

Efficient AlgorithmsQuicksort

Page 2: Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the

Quicksort• A common algorithm for sorting non-sorted arrays.• Worst-case running time is O(n2), which is actually the situation where the array is already sorted.• But the average-running time is O(n lg n) – with quite low constants. This makes it a nice algorithm.

• Divide and Conquer Algorithm

Page 3: Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the

Quicksort – de 3 dele• We are starting with an un-sorted arrayA[p…r]

• Divide: We start by dividing the array into two subarrays A[p…q-1] and A[q+1…r]. (for now, we just leave A[q]). We divide it such that all elements of A[p..q-1] are smaller than or equal to A[q], which again is smaller than or equal to A[q+1…r]. q is determined as a part of this step.

• Conquer: Sort the two arrays A[p..q-1] and A[q+1..r] by recursively calling quicksort.

• Combine: We are finished, since the array is now sorted.

The most difficult is probably ”Partition”, so here is an example:

Page 4: Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the

Partition

Page 5: Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the

…. And the main quicksort algorithm

….. We want to show that the algorithm works correctly, so we get back to the partition algorithm…..

Page 6: Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the

Partition - correctness

Page 7: Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the

Quicksort - performance• Worst-case – look at the tree….

• T(n) = T (n-1) + T(0) + (n)• Subst.metode: T(n)= (n2)

• Best-case (mostly for the intuition)• T(n) ≤ 2T(n/2) + (n)• case 2 master theorem (as before) T(n) = O(n lg n).

• The best-case analysis almost always hold….

• Examples on next slide: 9-1 split in all cases, and shifting between good and bad splits.

Page 8: Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the

Quicksort - performance

Analysis of 9-1 split:T(n) ≤ T(9n/10) + T(n/10)+ cn…. (because the height of the tree is (lg n)O(n lg n)

So: Shifting between good and bad splits still result in O(n lg n).But: It requires a closer and more formal analysis to really study average performance. This is described in Cormen – chapter 7.4.

Page 9: Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the

Randomized quick-sortWe could just permute the elements in the array by some random – algorithm. But it can be done simpler – we just select our pivot-entry randomly:

Page 10: Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the

Rest of the day…Until noon – Exercises/Problems:• Heapsort: 6.1-1, 6.1-2, 6.2-1, 6.2-6, 6.4-1• Quicksort: 7.1-1, 7.2-3• Recurrences: 4-4 (a,c,e,f,g,h,i)

Afternoon:•Self-study on Cormen, Chapter 5: Probabilistic Analysis and Randomized Algorithms (you can skip 5.4).

Page 11: Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the

Next week: Mini project• Implement Insertion sort, and test what is the running time for different sizes of inputs (write a program so that you can create randomized inputs as well as inputs on the form 1,2,3,…,n and n,…,3,2,1.).

• Implement Merge sort, and test the running time for different sizes of inputs – compare to the results of Insertion sort.

• Now, try to make an implementation of the Heap sort algorithm.

• Next – if you have time – the fun part. Make an implementation of the heap sort algorithm that is able to benefit from running on an n-core processor (you might choose to combine merge sort and heap sort).Who can make the fastest algorithm for large inputs of n?

•Volker will be available next week, and introduce the mini projects on Monday (you are not supposed to spend more time on in than what is scheduled).