Download presentation
Presentation is loading. Please wait.
Published byDeirdre Francis Modified over 9 years ago
1
Efficient Algorithms Quicksort
2
Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), 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
3
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:
4
Partition
5
…. And the main quicksort algorithm ….. We want to show that the algorithm works correctly, so we get back to the partition algorithm…..
6
Partition - correctness
7
Quicksort - performance Worst-case – look at the tree…. T(n) = T (n-1) + T(0) + (n) Subst.metode: T(n)= (n 2 ) 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.
8
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.
9
Randomized quick-sort We 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:
10
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).
11
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).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.