Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
4Activity
0 of .
Results for:
No results containing your search query
P. 1
Bucket-Sort (§ 4.5.1) Bucket-Sort and Radix-Sort

Bucket-Sort (§ 4.5.1) Bucket-Sort and Radix-Sort

Ratings: (0)|Views: 433|Likes:
Published by mr_mahesh_in

More info:

Published by: mr_mahesh_in on Oct 20, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

05/11/2014

pdf

text

original

 
Bucket-Sort and Radix-Sort1
Bucket-Sort and Radix-Sort
0123456789
 B
1,
c
7,
7,
 g 
3,
b
3,
a
7,
e
Bucket-Sort and Radix-Sort2
Bucket-Sort (§ 4.5.1)
Let be
 S 
be a sequence of 
n
(key, element) items with keysin the range
[0,
 N 
 
1]
Bucket-sort uses the keys asindices into an auxiliary array
 B
of sequences (buckets)
Phase 1: Empty sequence
 S 
bymoving each item
(
,
o
)
into itsbucket
 B
[
]
Phase 2: For
=
0,
 ,
 
 N 
 
1
, movethe items of bucket
 B
[
]
to theend of sequence
 S 
 Analysis:
Phase 1 takes
O
(
n
)
time
Phase 2 takes
O
(
n
 
+
 N 
)
time
Bucket-sort takes
O
(
n
 
+
 N 
)
time
Algorithm
 
bucketSort 
(
 S,
 
 N 
)
Input
 sequence
 S 
of (key, element)items with keys in the range[0,
 N 
 
1]
Output
 sequence
 S 
sorted byincreasing keys
 B
 
array of 
 N 
empty sequences
while
 
¬
 S.isEmpty
()
 f 
S.first 
()(
,
o
)
S.remove
(
 f 
)
 B
[
]
 .insertLast 
((
,
o
))
for
 0
to
 
 N 
 
1
while
 
¬
 B
[
]
 .isEmpty
() 
 f 
B
[
]
 .first 
()(
,
o
)
B
[
]
 .remove
(
 f 
)
 S.insertLast 
((
,
o
))
Bucket-Sort and Radix-Sort3
Example
Key range
[0, 9]7,
1,
c
3,
a
7,
 g 
3,
b
7,
e
1,
c
3,
a
3,
b
7,
7,
 g 
7,
e
Phase 1Phase 2
0123456789
 B
1,
c
7,
7,
 g 
3,
b
3,
a
7,
e
Bucket-Sort and Radix-Sort4
Properties and Extensions
Key-type Property
The keys are used asindices into an arrayand cannot be arbitraryobjects
No external comparator
Stable
Sort Property
The relative order of any two items with thesame key is preservedafter the execution of the algorithm
Extensions
Integer keys in the range
[
a
,
b
]
Put item
(
,
o
)
into bucket
 B
[
a
]
String keys from a set
 D
of possible strings, where
 D
hasconstant size (e.g., names of the 50 U.S. states)
Sort
 D
and compute the rank 
(
)
 
of each string
of 
 D
inthe sorted sequence
Put item
(
,
o
)
into bucket
 B
[
(
)]
Bucket-Sort and Radix-Sort5
Lexicographic Order
 A 
d-
tuple is a sequence of 
keys
(
1
,
2
,
,
)
, wherekey
is said to be the
i-
th dimension of the tuple
Example:
The Cartesian coordinates of a point in space are a 3-tuple
The lexicographic order of two
d-
tuples is recursivelydefined as follows
(
 x 
1
,
 x 
2
,
,
 x 
)
<
(
 y
1
,
 y
2
,
,
 y
)
 x 
1
<
 
 y
1
 
 x 
1
 
=
 
 y
1
 
(
 x 
2
,
,
 x 
)
<
(
 y
2
,
,
 y
)
I.e., the tuples are compared by the first dimension,then by the second dimension, etc.
Bucket-Sort and Radix-Sort6
Lexicographic-Sort
Let
be the comparatorthat compares two tuples bytheir
i-
th dimension
Let
 stableSort 
(
 S 
,
)
be astable sorting algorithm thatuses comparator
Lexicographic-sort sorts asequence of 
d-
tuples inlexicographic order byexecuting
times algorithm
 stableSort 
, one perdimension
Lexicographic-sort runs in
O
(
dT 
(
n
))
time, where
(
n
)
isthe running time of 
 stableSort 
Algorithm
 
lexicographicSort 
(
 S 
)
Input
 sequence
 S 
of 
-tuples
Output
 sequence
 S 
sorted inlexicographic order 
for
 
 
downto
1
 stableSort 
(
 S 
,
)
Example:
(7,4,6) (5,1,5) (2,4,6) (2, 1, 4) (3, 2, 4)(2, 1,4) (3, 2,4) (5,1,5) (7,4,6) (2,4,6) (2,1, 4) (5,1,5) (3,2, 4) (7,4,6) (2,4,6) (2, 1, 4) (2,4,6) (3, 2, 4) (5,1,5) (7,4,6)

Activity (4)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads
pnbdp liked this
Tanya Moldovan liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->