Professional Documents
Culture Documents
Bucket Radix
Bucket Radix
10/02/05
10/02/05
Yes
Strategy will not involve comparisons
BucketSor
t
Slide 3
Decision-tree example
Sort a1, a2, , an
1:2
2:3
123
1:3
213
1:3
132
312
2:3
231
321
Slide 4
Decision-tree example
Sort a1, a2,
a3 9, 4, 6
1:2
94
2:3
123
1:3
213
1:3
132
312
2:3
231
321
t
Slide 5
Decision-tree example
Sort a1, a2,
a3 9, 4, 6
1:2
2:3
123
1:3
213
1:3
132
312
96
2:3
231
321
t
Slide 6
Decision-tree example
Sort a1, a2,
a3 9, 4, 6
1:2
2:3
123
1:3
213
1:3
132
312
4 6 2:3
231
321
t
Slide 7
Decision-tree example
Sort a1, a2,
a3 9, 4, 6
1:2
2:3
123
1:3
213
1:3
132
312
2:3
231
321
469
Each leaf contains a permutation , ,, (n) to
indicate that the ordering a(1) a(2) a(n) BucketSor
has been
established.
t
10/02/05
Slide 8
Decision-tree model
A decision tree can model the execution of
any comparison sort:
One tree for each input size n.
View the algorithm as splitting whenever
it compares two elements.
The tree contains the comparisons along
all possible instruction traces.
The running time of the algorithm = the
length of the path taken.
BucketSor
Worst-case running time = height ofttree.
10/02/05
Slide 9
1:2
2:3
1:3
a[j] = a[j-1];
j--; }
123
213
1:3
2:3
a[j] = B; }}
132
10/02/05
312
231
321
BucketSor
t
Slide 10
Slide 11
Bucket sort
10/02/05
BucketSor
t
Slide 12
Example
4 2 1
10/02/05
2 0 3 2 1
0
0
0
1
1
0 0 0 1
2
2
2
2
4 0 2 3 0
3
3
4
4
2 2 2 2 3 3 BucketSor
4 4
t
Slide 13
// initialize m buckets
// place elements in their
// appropriate buckets
// place elements in buckets
// back in S
BucketSor
t
Slide 14
10/02/05
initialize m buckets
place in buckets
place elements in
buckets back in S
BucketSor
t
Slide 16
Time complexity
Bucket initialization: O( m )
From array to buckets: O( n )
From buckets to array: O( n )
10/02/05
Sorting integers
10/02/05
Can we do better?
BucketSor
t
Slide 18
Radix sort
10/02/05
20
10/02/05
12
52
63
64
37
36 47
20 88 47
58
18
88
9
99
20 12 52 63 64 36 37 47 58 18 88BucketSor
9 99
t
Slide 20
9
10/02/05
12
18
20
36
37
52
47 58
63
64
88
99
99
12 18 20 36 37 47 52 58 63 64BucketSor
88 99
t
Slide 21
20 88 47
20 12 52 63 64 36 37 47 58 18 88 9
99
10/02/05
12 18 20 36 37 47 52 58 63 64 88 99
BucketSor
t
Slide 22
10/02/05
BucketSor
t
Slide 23
Time complexity
10/02/05
10/02/05
Summary
10/02/05
BucketSor
t
Slide 26