Introduction - Aalborg Universitet
Download
Report
Transcript Introduction - Aalborg Universitet
Efficient Algorithms
Quicksort
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
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:
Partition
…. And the main quicksort algorithm
….. We want to show that the algorithm works correctly, so we get back
to the partition algorithm…..
Partition - correctness
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.
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.
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:
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).
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).