CSE 326: Data Structures: Sorting: Lecture 16: Friday, Feb 14, 2003
CSE 326: Data Structures: Sorting: Lecture 16: Friday, Feb 14, 2003
CSE 326: Data Structures: Sorting: Lecture 16: Friday, Feb 14, 2003
Sorting
1
Review: QuickSort
procedure quickSortRecursive (Array A, int left, int right)
/* partition A s.t.:
A[left], A[left+1], …, A[i] pivot
A[i+1], A[i+2], …, A[right] pivot
*/
2
Review: The Partition
i = left; j = right;
repeat { while (A[i] < pivot) i++;
while (A[j] > pivot) j--;
3
Review: The Partition
At the end:
pivot
Q: How are
these elements ? A: They are = pivot !
. . . Partition . . .
pivot
13
Decision tree to sort list A,B,C
A<B B<A
A<B B<A
C C
C<
C<
B< A<
A
B
A,B,C. A<B B,A,C. B<A
C<B C<A
C C
A< B<
C<B
C<
A,C,B. A B,C,A.
C,A,B. C,B,A
15
Max depth of the decision tree
• How many permutations are there of N numbers?
N!
• How many leaves does the tree have?
N!
• What’s the shallowest tree with a given number of leaves?
log(N!)
• What is therefore the worst running time (number of
comparisons) by the best possible sorting algorithm?
log(N!)
16
Stirling’s approximation
n
n
n! 2n
e
n
n
log(n !) log 2 n
e
At least one n n
branch in the log( 2 n ) log (n log n)
e
tree has this
depth
17
If you forget Stirling’s formula...
ln n ! ln1 ln 2 ... ln n
n
ln k ln x dx
n
1
k 1
x ln x 1 n ln n n 1
n
for j = 0 to N-1
Q[A[j]].enqueue(A[j]); Stable
sorting !
Result = new Queue;
for k = 0 to M-1
Result = Result.append(Q[k]);
return Result;
} 20
Bucket Sort
• Running time: O(M+N)
• Space: O(M+N)
• Idea: BucketSort on
each digit, bottom up.
23
The Magic of RadixSort
• Input list:
126, 328, 636, 341, 416, 131, 328
• BucketSort on lower digit:
341, 131, 126, 636, 416, 328, 328
• BucketSort result on next-higher digit:
416, 126, 328, 328, 131, 636, 341
• BucketSort that result on highest digit:
126, 131, 328, 328, 341, 416, 636
24
Inductive Proof that RadixSort Works
• Keys: d-digit numbers, base B
– (that wasn’t hard!)
• Claim: after ith BucketSort, least significant i digits
are sorted.
– Base case: i=0. 0 digits are sorted.
– Inductive step: Assume for i, prove for i+1.
Consider two numbers: X, Y. Say Xi is ith digit of X:
• Xi+1 < Yi+1 then i+1th BucketSort will put them in order
• Xi+1 > Yi+1 , same thing
• Xi+1 = Yi+1 , order depends on last i digits. Induction
hypothesis says already sorted for these digits because
BucketSort is stable
25
Radix Sort
26
Radix Sort
A= 35 53 55 33 52 32 25
Q[0] Q[1] Q[2] Q[3] Q[4] Q[5] Q[6] Q[7] Q[8] Q[9]
52 32 53 33 35 55 25
A= 52 32 53 33 35 55 25
Q[0] Q[1] Q[2] Q[3] Q[4] Q[5] Q[6] Q[7] Q[8] Q[9]
25 32 33 35 52 53 55
A= 25 32 33 35 52 53 55 27
Running time of Radixsort
• N items, D digit keys of max value M
• How many passes?
• How much work per pass?
• Total time?
28
Running time of Radixsort
• N items, D digit keys of max value M
• How many passes? D
• How much work per pass? N + M
– just in case M>N, need to account for time to empty out
buckets between passes
• Total time? O( D(N+M) )
29
Radix Sort
• What is the size of the input ? Size = DN
cD-1 cD-2 … c0
A[N-1]
A[1]
A[2]
A[3]
A[4]
33
Merge Sort Good for Something!
• Basis for most external sorting routines
• Can sort any number of records using a tiny
amount of main memory
– in extreme case, only need to keep 2 records in
memory at any one time!
34
External MergeSort
• Split input into two “tapes” (or areas of disk)
• Merge tapes so that each group of 2 records is
sorted
• Split again
• Merge tapes so that each group of 4 records is
sorted
• Repeat until data entirely sorted
log N passes
35
Better External MergeSort
• Suppose main memory can hold M records.
• Initially read in groups of M records and
sort them (e.g. with QuickSort).
• Number of passes reduced to log(N/M)
36