题目
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.
(b) Use the first-fit bin packing algorithm to allocate the boxes to the containers.
(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.
(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.
题目中文翻译
上面列表中的数字是十个箱子的重量(单位:千克)。箱子将用容器运输,每个容器最多可容纳 40 千克。
(a) 计算运输箱子所需容器数量的下界。必须展示计算过程。
(b) 使用首次适应装箱算法将箱子分配给容器。
(c) 使用提供的列表,执行快速排序以产生升序的重量列表。必须清楚标出枢轴。
(d) 使用二分搜索算法尝试在排序列表中定位重量 9。清楚说明如何选择枢轴以及每个阶段拒绝列表的哪一部分。
解答
(a)
解法一
思路
展开
先求十个箱子的总重量,再除以每个容器的容量 40。所得结果必须向上取整,因为不足一个完整容器的余量仍需另用一个容器。
答题过程
展开
The total weight is
Hence
Therefore the lower bound is
(b)
解法一
思路
展开
按原顺序逐个处理重量。每次都从 Container 1 开始,放入第一个尚有足够剩余容量的容器;只有现有容器都放不下时才开启新容器。
答题过程
展开
Applying first-fit in the given order gives:
| Container | Weights | Total |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 |
Thus the first-fit allocation is
(c)
解法一
思路
展开
采用 middle-right 规则选择枢轴。每次把小于枢轴的数放在左侧,大于枢轴的数放在右侧,再对尚未排好的子列表重复。下列竖线表示枢轴已经固定在最终位置。
答题过程
展开
Using middle-right pivots:
Initial pivot: .
Next pivots: and .
Next pivots: and .
Next pivot: .
解法二
思路
展开
也可始终采用 middle-left 规则。只要每个子列表都一致地使用这一规则,并正确完成每次划分,就会得到相同的升序列表。
答题过程
展开
Using middle-left pivots:
Initial pivot: .
Next pivot: .
Next pivots: and .
Next pivot: .
Finally use pivot on the remaining sublist :
(d)
解法一
思路
展开
在升序列表中使用 middle-right 元素作枢轴。将目标值 9 与枢轴比较,每次排除不可能含有 9 的一半,直至找到 9。
答题过程
展开
The sorted list is
For positions to , the middle-right position is , so the pivot is . Since , reject to .
For positions to , the middle-right position is , so the pivot is . Since , reject to .
For positions to , the middle-right position is , so the pivot is . Since , reject .
The only remaining value is at position :