lect18-sorting-review(notcur)

lect18-sorting-review(notcur) - Lecture Notes CMSC 251 Here...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture Notes CMSC 251 Here is an example. 576 49[4] 9[5]4 [1]76 176 494 19[4] 5[7]6 [1]94 194 194 95[4] 1[7]6 [2]78 278 296 = 57[6] = 2[7]8 = [2]96 = 296 278 29[6] 4[9]4 [4]94 494 176 17[6] 1[9]4 [5]76 576 954 27[8] 2[9]6 [9]54 954 The running time is clearly Θ( d ( n + k )) where d is the number of digits, n is the length of the list, and k is the number of values a digit can have. This is usually a constant, but the algorithm’s running time will be Θ( dn ) as long as k O ( n ) . Notice that we can be quite flexible in the definition of what a “digit” is. It can be any number in the range from 1 to cn for some constant c , and we will still have an Θ( n ) time algorithm. For example, if we have d =2 and set k = n , then we can sort numbers in the range n * n = n 2 in Θ( n ) time. In general, this can be used to sort numbers in the range from 1 to n d in Θ( dn ) time. At this point you might ask, since a computer integer word typically consists of 32 bits (4 bytes), then doesn’t this imply that we can sort any array of integers in O ( n ) time (by applying radix sort on each of the d =4 bytes)? The answer is yes, subject to this word-length restriction. But you should be careful about attempting to make generalizations when the sizes of the numbers are not bounded. For example, suppose you have n keys and there are no duplicate values. Then it follows that you need at least B = d lg n e bits to store these values. The number of bytes is d = d B/ 8 e . Thus, if you were to apply radix sort in this situation, the running time would be Θ( dn )=Θ ( n log n ) . So there is no
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 2

lect18-sorting-review(notcur) - Lecture Notes CMSC 251 Here...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online