東京大学 情報理工学系研究科 コンピュータ科学専攻 2019年2月実施 問題2
Author
kainoj, 祭音Myyura
Description
Consider the Java program below to sort an array in an ascending order. , , and are positive integers, and is an array of nonnegative integers where for all .
In this program, list is a class of an integer list with the following methods:
lst.size(): returns the number of elements in the listlst.lst.get(i): returns the element at the -th position in the listlst(the position number starts from 0).lst.insert(i, x): inserts to the listlstat the -th position.
is an array of size , whose elements are all initialized to empty lists. Suppose that the execution time of each of the above methods is constant. You can ignore overflow errors.
void mysort(int M, int N, int K, int[] A, list[] B) {
for (int i = 0; i < N; i++) {
int m = A[i] * K / M;
int j = 0;
for (; j < B[m].size(); j++) {
if (A[i] <= B[m].get(j)) {
break;
}
}
B[m].insert(j, A[i]);
}
int i = 0;
for (int m = 0; m < K; m++) {
for (int j = 0; j < B[m].size(); j++) {
A[i] = [ blank X ];
i = i + 1;
}
}
}
Answer the following questions:
(1) Answer an appropriate expression to fill the blank .
(2) Let be the number of times the line is executed. Answer the largest value of in terms of and . Also, answer the expected value of in terms of and , assuming that is distributed independently uniformly randomly over the set . Suppose that for this question.
(3) Explain how the expected running time of this program varies when changes, assuming that is distributed independently uniformly randomly.
(4) Discuss advantages and disadvantages of this algorithm in comparison to the quicksort algorithm.
题目描述
题中 Java 程序用于将数组 按升序排序。 为正整数,
含 个非负整数,且对所有 均有
。list 是整数列表类,size() 返回元素数,
get(i) 返回从 开始编号的第 个元素,insert(i,x) 在第 个位置插入
;这些方法均视为常数时间。 是长度为 的数组,每个元素初始为空列表。
忽略整数溢出。
程序先按
把每个元素分配到桶 ,并通过顺序扫描把它插入桶内的有序位置;随后按桶号从小到大把各桶元素写回 。回答下列问题。
(1)给出填入空白 的合适表达式,使程序正确写回排序结果。
(2)令 为程序第 6 行的执行次数。用 表示 的最大值;再假设每个 独立地在 上均匀分布,并假设 ,求 的期望。
(3)在上述独立均匀分布假设下,说明程序的期望运行时间如何随 改变。
(4)与快速排序相比,讨论该算法的优点和缺点。
Kai
Setting of the problem: we got element and buckets.
(1)
B[m].get(j)
(2)
The input sequence might be in an increasing order and all elements might fall into one bucket. Thus, line will be executed:
times. The input must be increasing because elements are inserted at the beginning of a bucket when they are no larger than its first element. Thus, to make every new element traverse the whole bucket, all elements may be placed in one bucket in increasing order.
For the expectation, let denote the size of bucket , and let
Then . Since each bucket has probability approximately when ,
and hence
Sorted insertion into bucket takes a number of comparisons of order in total. Summing over the buckets gives
More precisely, let be the number of the first elements in the bucket of . Ties are negligible, so . Conditional on ,
Hence
(3)
The following operations contribute to the expected running time:
- finding a bucket and inserting each element: ;
- scanning the buckets and writing their elements back: .
Thus
Increasing reduces collisions until , where the expected time is minimized at . For larger , scanning the empty buckets makes the term dominant.
(4)
- With and uniformly distributed bounded keys, this algorithm has expected time, compared with quicksort's expected .
- Its worst-case time is , and poor bucket balance degrades its performance.
- It needs auxiliary space and a known bounded key range; quicksort applies to general comparable keys and is usually in-place apart from its recursion stack.
- This implementation is not stable: the test
A[i] <= B[m].get(j)inserts a new equal key before earlier equal keys.