23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

itself is a sequence (of entries with key k). After <strong>in</strong>sert<strong>in</strong>g each entry of the <strong>in</strong>put<br />

sequence S <strong>in</strong>to its bucket, we can put the entries back <strong>in</strong>to S <strong>in</strong> sorted order by<br />

enumerat<strong>in</strong>g the contents of the buckets B[0], B[1],…,B[N − 1] <strong>in</strong> order. We<br />

describe the bucket-sort algorithm <strong>in</strong> Code Fragment 11.8.<br />

Code Fragment 11.8: Bucket-sort.<br />

It is easy to see that bucket-sort runs <strong>in</strong> O(n + N) time <strong>and</strong> uses O(n + N) space.<br />

Hence, bucket-sort is efficient when the range N of values for the keys is small<br />

compared to the sequence size n, say N = O(n) or N = O(nlogn). Still, its<br />

performance deteriorates as N grows compared to n.<br />

An important property of the bucket-sort algorithm is that it works correctly even if<br />

there are many different elements with the same key. Indeed, we described it <strong>in</strong> a<br />

way that anticipates such occurrences.<br />

Stable Sort<strong>in</strong>g<br />

When sortt<strong>in</strong>g key-value pairs, an important issue is how equal keys are h<strong>and</strong>led.<br />

Let S = ((k 0 ,x 0 ),…,(k n−1 ,x n−1 )) be a sequence of such entries. We say that a sort<strong>in</strong>g<br />

algorithm is stable if, for any two entries (k i,x i ) <strong>and</strong> (k j ,x j ) of S, such that k i = k j<br />

<strong>and</strong> (k i ,x i ) precedes (k j ,x j ) <strong>in</strong> S before sort<strong>in</strong>g (that is, i < j), entry (k i ,x i ) also<br />

precedes entry (k j ,x j ) after sort<strong>in</strong>g. Stability is important for a sort<strong>in</strong>g algorithm<br />

because applications may want to preserve the <strong>in</strong>itial order<strong>in</strong>g of elements with<br />

the same key.<br />

Our <strong>in</strong>formal description of bucket-sort <strong>in</strong> Code Fragment 11.8 does not guarantee<br />

stability. This is not <strong>in</strong>herent <strong>in</strong> the bucket-sort method itself, however, for we can<br />

easily modify our description to make bucket-sort stable, while still preserv<strong>in</strong>g its<br />

O(n + N) runn<strong>in</strong>g time. Indeed, we can obta<strong>in</strong> a stable bucket-sort algorithm by<br />

always remov<strong>in</strong>g the first element from sequence S <strong>and</strong> from the sequences B[i]<br />

dur<strong>in</strong>g the execution of the algorithm.<br />

711

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

Saved successfully!

Ooh no, something went wrong!