04.02.2018 Views

Algorithms

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

710 CHAPTER 5 ■ Strings<br />

J<br />

6<br />

A<br />

A<br />

K<br />

J<br />

Q<br />

6<br />

J<br />

A<br />

9<br />

9<br />

8<br />

9<br />

K<br />

4<br />

5<br />

Q<br />

3<br />

2<br />

10<br />

9<br />

7<br />

4<br />

4<br />

10<br />

A<br />

5<br />

3<br />

8<br />

2<br />

K<br />

4<br />

7<br />

Q<br />

J<br />

6<br />

3<br />

7<br />

8<br />

10<br />

3<br />

10<br />

7<br />

Q<br />

2<br />

2<br />

5<br />

K<br />

5<br />

6<br />

8<br />

K<br />

J<br />

9<br />

5<br />

2<br />

A<br />

3<br />

4<br />

6<br />

7<br />

8<br />

10<br />

Q<br />

6<br />

A<br />

J<br />

9<br />

3<br />

7<br />

4<br />

8<br />

Q<br />

10<br />

2<br />

K<br />

5<br />

A<br />

Q<br />

9<br />

8<br />

4<br />

10<br />

5<br />

K<br />

J<br />

3<br />

7<br />

2<br />

6<br />

J<br />

6<br />

A<br />

K<br />

Q<br />

10<br />

9<br />

4<br />

2<br />

7<br />

3<br />

5<br />

8<br />

A<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

J<br />

Q<br />

K<br />

A<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

J<br />

Q<br />

K<br />

A<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

J<br />

Q<br />

K<br />

A<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

J<br />

Q<br />

K<br />

Sorting a card deck with<br />

MSD string sort<br />

MSD st r ing sor t To implement a general-purpose string sort, where<br />

strings are not necessarily all the same length, we consider the characters<br />

in left-to-right order. We know that strings that start with a should appear<br />

before strings that start with b, and so forth. The natural way to implement<br />

this idea is a recursive method known as most-significant-digit-first (MSD)<br />

string sort. We use key-indexed counting to sort the strings according to<br />

their first character, then (recursively) sort the subarrays corresponding to<br />

each character (excluding the first character, which we know to be the same<br />

for each string in each subarray). Like quicksort, MSD string sort partitions<br />

the array into subarrays that can be sorted independently to complete<br />

the job, but it partitions the array into one subarray for each possible value<br />

of the first character, instead of the two or three partitions in quicksort.<br />

End-of-string convention. We need to pay particular attention to reaching<br />

the ends of strings in MSD string sort. For a proper sort, we need the subarray<br />

for strings whose characters<br />

have all been examined to<br />

appear as the first subarray, and<br />

we do not want to recursively<br />

sort this subarray. To facilitate<br />

these two parts of the computation<br />

we use a private twoargument<br />

toChar() method to<br />

convert from an indexed string<br />

character to an array index<br />

that returns -1 if the specified<br />

character position is past the<br />

end of the string. Then, we just<br />

add 1 to each returned value, to<br />

get a nonnegative int that we<br />

can use to index count[]. This<br />

convention means that we have<br />

R+1 different possible character<br />

values at each string position:<br />

0 to signify end of string,<br />

1 for the first alphabet character,<br />

2 for the second alphabet<br />

character, and so forth. Since<br />

sort on first character value<br />

to partition into subarrays<br />

0<br />

0<br />

0<br />

.<br />

.<br />

.<br />

0<br />

0<br />

1<br />

1<br />

1<br />

.<br />

.<br />

.<br />

1<br />

1<br />

2<br />

2<br />

2<br />

.<br />

.<br />

.<br />

2<br />

2<br />

r<br />

r<br />

r<br />

.<br />

.<br />

.<br />

r<br />

r<br />

.<br />

.<br />

.<br />

.<br />

.<br />

.<br />

Overview of MSD string sort<br />

recursively sort subarrays<br />

(excluding first character)<br />

0<br />

0<br />

0<br />

.<br />

.<br />

.<br />

0<br />

0<br />

1<br />

1<br />

1<br />

.<br />

.<br />

.<br />

1<br />

1<br />

2<br />

2<br />

2<br />

.<br />

.<br />

.<br />

2<br />

2<br />

r<br />

r<br />

r<br />

.<br />

.<br />

.<br />

r<br />

r<br />

.<br />

.<br />

.<br />

.<br />

.<br />

.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!