Uploaded by Samuel Bangapadang

lga-14-linear-time-sorting

advertisement
Data Structures & Algorithms
Learning group assignment #14
Linear Time Sorting
All students should watch the videos accompanying this LGA in preparation for this assignment and the
next lecture.
Individual self-test questions
All students should answer this set of questions. You will compare your answers with your group during
the next lecture period. Note, we may from time to time include trick questions.
• True/False: radix sort is a linear-time algorithm for all inputs. Why or why not?
• Binsort, counting sort, and radix sort achieve linear-time sorting by directly assigning values to containers (typically called
) according to some key property of the values. Crucially,
pairs of values from the input to each other, like previous sorts we
these sorts do not
have studied.
Group questions
Distribute the following questions across the members of your group. You will share your solutions (and
most importantly the method of your solutions) during the next lecture period. Divide up the questions so
that each question has at least two solutions from different group members.
1. (Basic) Trace through a run of the counting sort algorithm (pseudo-code is given in the lecture slides,
or see the Wikipedia entry) to sort the array [3, 2, 1, 4, 3, 3, 2, 4, 5, 2, 5] (note k = 5). Show the count
array after the first for loop, the count array after the second for loop, and the final output array.
Page 1 of 4
Data Structures & Algorithms
Learning group assignment #14
Linear Time Sorting
2. (Basic) Trace through a run of radix sort to sort the array [957, 143, 651, 227, 557, 221, 127] using 10
buckets per pass. Show the order of the values after each pass.
Page 2 of 4
Data Structures & Algorithms
Learning group assignment #14
Linear Time Sorting
3. (Intermediate) Suppose we want to sort 1 million, 9-digit non-negative integers. We want to use radix
sort with counting sort as our stable sort, but we want to decide on three different strategies:
• Apply counting sort on 1 digit at a time.
• Apply counting sort on 3 digits at a time.
• Apply counting sort on 9 digits at a time.
Questions:
(a) Which of these should be fastest? Hint: Consider the number of passes required as well as the
cost of each pass.
(b) Which of these uses the least amount of memory?
Page 3 of 4
Data Structures & Algorithms
Learning group assignment #14
Linear Time Sorting
4. (Intermediate) Repeating a question from the lecture: suppose you want to sort n = 100, 000 strings
whose maximum length is 40. Is radix sort preferable to merge sort for this problem?
Page 4 of 4
Download