東京大学 情報理工学系研究科 コンピュータ科学専攻 2019年2月実施 問題2
Author
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. Note that it has to be an increasing order, because items are being inserted at the beginning of a bucket. If we wanna do evil, we must make every item traverse the whole bucket, until the end. It is possible when every inserted item is bigger than any element in the bucket, that is, input sequence is of increasing order.
(3)
Let denote size of -th bucket. Line is de facto an insertion sort, which means that -th~bucket will be sorted in . Total running time will be to:
Taking expectation:
What is ? Note that, by definition for any random variable . Let be an indicator random variable:
Since we have buckets and every of them is equally likely, probability of "going to bucket " is . Thus, expectation of is:
We can also notice, that, is just a binomial random variable with expectation and variance . Here a trial is mapping an item into bucket, and the success is placing it into -th bucket. There are trials and probability of success if .
Finally:
The following contribute to total expected running time:
- finding a bucket for each element
- sorting each bucket,
- for each bucket, getting its content:
Total running time:
When is , then we get expected running time. When is , then the running time is .
(4)
- BS is stable, QS isn't
- BS isn't in-place, QS is
- BS runs expected, QS is
- BS has upper limit on keys, QS hasn't