题目
Problem
1791582013284125
The numbers in the list shown above are the weights, in kilograms, of ten boxes. The boxes are to be transported in containers that will each hold a maximum weight of 40 kilograms.
(a) Calculate a lower bound for the number of containers that will be needed to transport the boxes. You must show your working.
(2)
(b) Use the first-fit bin packing algorithm to allocate the boxes to the containers.
(3)
(c) Using the list provided, carry out a quick sort to produce a list of the weights in ascending order. You must make your pivots clear.
(3)
(d) Use the binary search algorithm to try to locate the weight of 9 in the sorted list. Clearly indicate how you choose your pivots and which part of the list is being rejected at each stage.
(3)
题目中文翻译
1791582013284125
上面列表中的数字是十个箱子的重量(单位:千克)。箱子将用容器运输,每个容器最多可容纳 40 千克。
(a) 计算运输箱子所需容器数量的下界。必须展示计算过程。
(b) 使用首次适应装箱算法将箱子分配给容器。
(c) 使用提供的列表,执行快速排序以产生升序的重量列表。必须清楚标出枢轴。
(d) 使用二分搜索算法尝试在排序列表中定位重量 9。清楚说明如何选择枢轴以及每个阶段拒绝列表的哪一部分。
解答
(a)
解法一
思路
展开
先求十个箱子的总重量,再除以每个容器的容量 40。所得结果必须向上取整,因为不足一个完整容器的余量仍需另用一个容器。
答题过程
展开
The total weight is
=17+9+15+8+20+13+28+4+12+5131.
Hence
40131=3.275.
Therefore the lower bound is
4 containers.
(b)
解法一
思路
展开
按原顺序逐个处理重量。每次都从 Container 1 开始,放入第一个尚有足够剩余容量的容器;只有现有容器都放不下时才开启新容器。
答题过程
展开
Applying first-fit in the given order gives:
| Container | Weights | Total |
|---|
| 1 | 17,9,8,4 | 38 |
| 2 | 15,20,5 | 40 |
| 3 | 13,12 | 25 |
| 4 | 28 | 28 |
Thus the first-fit allocation is
C1:17,9,8,4;C2:15,20,5;C3:13,12;C4:28.
(c)
解法一
思路
展开
采用 middle-right 规则选择枢轴。每次把小于枢轴的数放在左侧,大于枢轴的数放在右侧,再对尚未排好的子列表重复。下列竖线表示枢轴已经固定在最终位置。
答题过程
展开
Using middle-right pivots:
Initial pivot: 13.
9,8,4,12,5∣13∣17,15,20,28
Next pivots: 4 and 20.
4∣9,8,12,5∣13∣17,15∣20∣28
Next pivots: 12 and 15.
4∣9,8,5∣12∣13∣15∣17∣20∣28
Next pivot: 8.
4,5,8,9,12,13,15,17,20,28
Sort complete.
解法二
思路
展开
也可始终采用 middle-left 规则。只要每个子列表都一致地使用这一规则,并正确完成每次划分,就会得到相同的升序列表。
答题过程
展开
Using middle-left pivots:
Initial pivot: 20.
17,9,15,8,13,4,12,5∣20∣28
Next pivot: 8.
4,5∣8∣17,9,15,13,12∣20∣28
Next pivots: 4 and 15.
4∣5∣8∣9,13,12∣15∣17∣20∣28
Next pivot: 13.
4∣5∣8∣9,12∣13∣15∣17∣20∣28
Finally use pivot 9 on the remaining sublist 9,12:
4,5,8,9,12,13,15,17,20,28.
(d)
解法一
思路
展开
在升序列表中使用 middle-right 元素作枢轴。将目标值 9 与枢轴比较,每次排除不可能含有 9 的一半,直至找到 9。
答题过程
展开
The sorted list is
4,5,8,9,12,13,15,17,20,28.
For positions 1 to 10, the middle-right position is 6, so the pivot is 13. Since 9<13, reject 13 to 28.
For positions 1 to 5, the middle-right position is 3, so the pivot is 8. Since 9>8, reject 4 to 8.
For positions 4 to 5, the middle-right position is 5, so the pivot is 12. Since 9<12, reject 12.
The only remaining value is at position 4:
9 is found.