Chap 7
Chap 7
7.1 Introduction.
– Insertion Sort.
– Heapsort.
– Mergesort.
– Quicksort.
7.1 Introduction
Example :
(8,1,6,3,6,4) (1,3,4,6,6,8)
Sorting Algorithm
Insertion Sort
Heapsort
1st Method
Heapsort
2nd Method
After the last deleteMin the array will contain the elements
in decreasing order.
97
53 59
26 41 58 31
97 53 59 26 41 58 31
0 1 2 3 4 5 6 7 8 9 10
First deleteMax
59
53 58
26 41 31 97
59 53 58 26 41 31 97
0 1 2 3 4 5 6 7 8 9 10
Mergesort
Recursive algorithm :
Merging two sorted lists can be done in one pass through the
input, if the output is put in a third list. At most
comparisons are made.
Analysis of Mergesort
N
T(N) = 2T(N/2) + cN
T(N) T(N/2) c
= +
N N/2
N/2 N/2
T(N/2) T(N/4) c
= +
N/2 N/4
N/4 N/4 N/4 N/4
log N T(N/4) T(N/8) c
= +
N/4 N/8
T(2) T(1) c
= +
2 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
T(N) T(1) c log N
= +
N 1
T(N) = cN log N+ N
= O(N log N)
2. If
, then .
3. If
and
, and if for
some then .
Quicksort
1
2
3
4
select pivot
13 81 92 43 31 57 26 65 75 0
partition
pivot
13 0 43 26 31 57 65 81 75 92
0 13 26 31 43 57 65 75 81 92
0 13 26 31 43 57 65 75 81 92
Quicksort Implementation
8 1 4 9 6 9 5 2 7 0
The pivot is 6.
8 1 4 9 0 3 5 2 7 6
i j pivot
1st swap
8 1 4 9 0 3 5 2 7 6
i j
2nd swap
2 1 4 9 0 3 5 8 7 6
i j
Last swap
2 1 4 5 0 3 9 8 7 6
j i
Result A[p ... i-1] A[i+1 ... r]
2 1 4 5 0 3 6 8 7 9
j i
Quicksort Routines
8 1 4 9 6 3 5 2 7 0
1st swap
8 1 4 9 6 3 5 2 7 0
2nd swap
6 1 4 9 8 3 5 2 7 0
3rd swap
0 1 4 9 8 3 5 2 7 6
Last swap
0 1 4 9 6 3 5 2 7 8
Result
0 1 4 9 7 3 5 2 6 8
i j
else
/*9 */ break;
/* 10*/ swap(a[i], a[right1]); // Restore pivot
/* 11*/ quicksort(a, left, i1); // Sort small elements
/* 12*/ quicksort(a, i+1, right); // Sort large elements
else // Do an insertion sort on the subarray
/*13 */ insertionSort(a, left, right);
/*9 */ break;
Analysis of Quicksort
i N-i-1
pivot
Assumptions :
Random pivot.
No cutoff for small arrays.
Malek Mouhoub, CS340 Fall 2002 23
Analysis of Quicksort
Worst-case Analysis
T(N) = T(N-1) + cN
N-1
pivot
T(N-1) = T(N-2) + c(N-1)
pivot N-2
T(N-2) = T(N-3) + c(N-2)
N
pivot 2
T(2) = T(1) + c(2)
pivot N
1 T(N) = T(1) + cΣ i
i=2
N
T(N) = 2T(N/2) + cN
T(N) T(N/2) c
= +
N N/2
N/2 N/2
T(N/2) T(N/4) c
= +
N/2 N/4
N/4 N/4 N/4 N/4
log N T(N/4) T(N/8) c
= +
N/4 N/8
T(2) T(1) c
= +
2 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
T(N) T(1) c log N
= +
N 1
T(N) = cN log N+ N
= O(N log N)
Assumptions :
The possible sizes of the subarrays have the same probability (1/N where N is the number of
elements of the array).
(1)
(2)
(3)
(4)
(5)
.
.
.
N/10 9N/10 N
log N
logN 10
10/9 N/100 9N/100 9N/100 81N/100 N
1 81N/1000 729N/1000 N
<= N
<= N
1
O(N log N)
Bucketsort
Bucketsort
General sorting algorithms using only comparisons require time
in the worst case.
Radix sort
Input : the keys are all nonnegative integers in base 10 and having the
same number of digits.
– Method 1 : Sort on the most significant digit first (leftmost digit first).
The ith step of the method consists in distributing the keys into
distinct piles based on the values of the ith digit from the left.
Prove that any algorithm for sorting that uses only comparisons
requires
Decision Trees
eorem 1 Any sorting algorithm that uses only comparisons between elements
requires at least comparisons in the worst case.
eorem 2 Any sorting algorithm that uses only comparisons between elements
requires comparisons.
Multi-way Merge
–
–
– (Idea of the
heapsort).
in the
worst case. Why ?
1
2
3 If then
5 Else If (k>q)
6 Else return
/* 11*/ if (k = i) 20
/* 12*/ quickSelect(a, left, i 1, k);
/* 13*/ else if (k i + 1)
/* 14*/ quickSelect(a, i+1, right, k);
/* 15*/ else return a[k]
else // Do an insertion sort on the subarray
/*16 */ insertionSort(a, left, right);
1 if p=r
2 then
return
3
4
5 If
6 then return
7 else return
produces a partition whose low side has 1 element with
probability and elements with probability for .
The recurrence can be solved by substitution (assuming that for some constant
) :