Lecture 12 AG
Lecture 12 AG
Fundamental Structures
of Computer Science § Homework #4 is available
Ø Due on Monday, March 17, 11:59pm
Introduction to Sorting Ø Get started now!
§ Quiz #2
Ø Available on Tuesday, Feb.25
Ø Some questions will be easier if you have
some parts of HW4 working
1
Insertion sort Insertion sort
105 47 13 99 30 222
for i = 1 to n-1 do
47 105 13 99 30 222
insert a[i] in the proper place
13 47 105 99 30 222
in a[0:i-1]
13 47 99 105 30 222
13 30 47 99 105 222
Invariant 1: A[0..i-1] is a sorted permutation of the original A[1..i-1] § In the ith step we do at least 1
j = i-1; key = A[i]; comparison, at most (i-1) comparisons
while (j >= 0 && A[j]>key)
and on average i/2 (call this Ci)
{ Invariant 2: A[j .. i-1] are all larger than the key
A[j+1] = A[j-- ] ; § Mi – the number of moves at the ith step is
} C i_+ 2
A[j] = key;
2
Heapsort N2 vs Nlog N
§ Remember heaps:
ØbuildHeap has O(n) worst-case running
time. That is Σ 2i(h-i) = O(n)
ØdeleteMin has O(log n) worst-case
running time. So n deleteMin’s would
give a sorted list. N^2
Nlog N
§ Heapsort :
ØBuild heap. O(n)
ØDeleteMin until empty. O(n log n)
ØTotal worst case: O(n log n)
3
Shell Sort Analysis Shell Sort Analysis ctd..
§ Each pass benefit from previous § It is shown that for the sequence
ØEach i-sort combines two groups sorted 1,3,7,15,31,… given by
in previous 2i-sort. Øht=1, hk-1=2hk+1 and t = logn –1
§ Any sequence of increments ØFor this sequence, Shell Sort Algorithm
(h1,h2,…) are fine as long as last one is O(n1.2)
is 1. ØProof available but difficult. Ignore till
Øht = 1, hi+1 < hi 15-451.
ØEach h-sort is an insertion sort
§ Very difficult mathematical analysis
§ Then:
4
Recurrence relation A solution
§ A solution for
§ Then such “recursive sorting” is ØT(1) = 1
characterized by the following ØT(N) = 2T(N/2) + N
recurrence relation:
§ is given by
ØT(N) = Nlog N + N
ØT(1) = 1
ØT(n) = 2 T(n/2) + n Øwhich is O(Nlog N).
5
Mergesort Mergesort
But
don’t
actually
want to
create
all of
these
arrays!
6
Upper-bounds Upper-bounds
§ Corollary:
§ Base case:
ØT(1) = 1log 1 + 1 = 1
§ Inductive case:
§ It is also useful sometimes to check ØAssume T(M) = Mlog M + M, all M<N.
that a solution is valid. ØT(N) = 2T(N/2) + N
7
Quicksort Quicksort idea
§ Recurse on each
half and conquer!
8
Quicksort algorithm Doing quicksort in place
105 47 13 17 30 222 5 19
85 24 63 50 17 31 96 45
19
5 17 13 47 30 222 105 85 24 63 45 17 31 96 50
13 47 L R
5 17 30 222 105
85 24 63 45 17 31 96 50
105 222 L R
31 24 63 45 17 85 96 50
In practice, insertion sort is used once the arrays
get “small enough”. L R
31 24 17 45 50 85 96 63
9
Analysis of quicksort Worst-case analysis
§ In the best case, the pivot is always § Consider the quicksort tree:
the median element.
105 47 13 17 30 222 5 19
§ The time spent at each level of the § A fast sorting algorithm in practice.
tree is O(N).
§ Can be implemented in-place.
§ So, on average, how many levels?
ØThat is, what is the expected height of § But is O(N2) in the worst case.
the tree?
ØIf on average there are O(log N) levels,
then quicksort is O(Nlog N) on average. § Average-case performance?
10