東京大学 工学系研究科 電気系工学専攻 2024年8月実施 問題4 情報工学II
Author
Description
I.
TODO
II.
Consider the following algorithm , which overwrites an input array of distinct non-negative integers to sort its components in ascending order:
- for or , do nothing.
- for , do nothing if , and overwrite as , otherwise.
- for , select an element of by using a function as . Let be an array consisting of all the elements in being smaller than , and be an array consisting of all the elements in being greater than . Overwrite as . Then, recursively apply to the subarrays and in as and , respectively.
Answer the following questions.
(1) Fig. 8 exemplifies the operation of for an array when and the order of the elements in and inherits their order in . Draw the operation of for by following the diagram in Fig. 8.
(2) Suppose that an array contains each integer from 1 to 9, once each. Also, assume that and the order of the elements in and inherits their order in . Obtain the array that maximizes the number of recursive calls of and has the largest value evaluated as a 9-digit integer. Also obtain the array that minimizes the number of recursive calls of and has the largest value evaluated as a 9-digit integer. Note that the value of evaluated as a 9-digit integer is 123456789.
(3) Assume that . For an array with length , obtain the order of worst-case time complexity by supplementing the intermediate steps of its derivation. In the derivation, evaluate the order of the time complexity in terms of comparisons, while ignoring the complexity of the other operations.
(4) Program 1 is an implementation of the algorithm in the C programming language. Describe the codes that should be in the blanks, [A], [B], and [C].
(5) Suppose that is a function that returns an element selected from uniformly and randomly. Answer the order of the average time complexity of as a function of . In addition, describe its reason in detail.
Fig. 8
(6,9,3,5,1,7)
|
(3,5,1) -- 6 - (9,7)
| | |
(1) - 3 - (5) | (7,9)
|____|____| | |
| | |
(1,3,5) -- 6 - (7,9)
|
(1,3,5,6,7,9)
/* Program1 */
void swap(int X[], int i, int j){
int tmp = X[i]; X[i] = X[j]; X[j] = tmp;
}
int partition (int X[], int left, int right) {
int pivot = X[left];
int i = right;
for (int j = right; j >= left+1; j--) {
if (X[j] >= pivot) {
[A]
i--;
}
}
[B]
return i;
}
void f(int X[], int left, int right) {
if (left < right) {
int pivotpos = partition(X, left, right);
[C]
}
}
int main (void) {
int X[10] = {9, 6, 1, 7, 2, 3, 4, 5, 0, 8};
f(X, 0, sizeof(X) / sizeof(X[0]) - 1);
return 0;
}
题目描述
原 Description 的第一部分只有 TODO,Kai 的对应部分也只有 TODO,因此本地内容无法确认第一部分的具体题干。
第二部分定义递归排序算法 。输入是由 个互不相同的非负整数组成的数组 : 时不操作; 时按升序交换; 时由 选枢轴,把小于 的元素组成 、大于 的元素组成 ,将数组改写为 ,再分别递归排序 。
- 当 ,且划分后各元素保持原相对次序时,仿照图 8,完整画出 的递归划分与最终排序过程。
- 恰含整数 至 各一次,仍取首元素为枢轴并保持相对次序。求使递归调用次数最多、且作为九位整数时数值最大的数组 ;再求使递归调用次数最少且九位整数值最大的数组 。
- 当 时,以元素比较次数为准,补出递推推导的中间步骤并求长度为 的数组在最坏情况下的时间复杂度阶。
- 补全给定 C 程序中
partition的 [A]、[B] 和递归函数f的 [C],使程序实现上述划分与排序。 - 若 从 中均匀随机选取枢轴,给出 的平均时间复杂度阶,并详细说明随机划分、递归树高度和每层工作量如何导出该结论。
Kai
I.
TODO
II.
(1)
(6,8,4,5,2,3,1,9,7)
|
(4,5,2,3,1) -- 6 -- (8,9,7)
| | |
(2,3,1) --- 4 - (5) | (7) - 8 - (9)
| | | | |____|____|
(1) - 2 - (3) | | | |
|____|____| | | | (7,8,9)
| | | | |
(1,2,3) | | | |
|_______|____| | |
| | |
(1,2,3,4,5) | |
|_______|_______|
|
(1,2,3,4,5,6,7,8,9)
(2)
The worst case is when the partition is as unbalanced as possible.
Since we want the digits at the start of the array to be as large as possible.
In addition, each integer from 1 to 9 is one each.
Therefore, the worst case is 987654321, i.e. .
For , we want this case: (from bottom to top)
(1,2,3,4,5,6,7,8,9)
_______|_________
| | |
(1,2,3,4) 5 (6,7,8,9)
_____|____ | _____|____
| | | | | | |
(1,2) - 3 - (4) | (6,7) - 8 - (9)
| | |
(3,4,2,1) - 5 --- (8,9,7,6)
|
(5,8,9,7,6,3,4,2,1)
Therefore, the best and largest case is 589763421.
(Note to readers: The C code provided in the question is unstable; it will swap the Pivot to the middle and swap the elements that were originally at the end to the beginning)
(3)
In the worst case, the time complexity
Since is constant, therefore
i.e. , i.e. the time complexity is
(4)
- A:
swap(X, i, j); - B:
swap(X, left, i); - C:
f(X, left, pivotpos-1); f(X, pivotpos+1, right)
(5)
Complexity:
Reason:
On average, the randomly chosen pivots splits the array into two subarrays of roughly proportional sizes. Because balanced or near-balanced splits occur with constant probability, the expected height of the recursion tree becomes logarithmic, i.e. . Since the partitioning operation at each level of recursion trees takes time in total, the total expected time complexity is .
The expected running time satisfies: i.e.