题目
Use the binary search algorithm to try to locate the word “Parallelogram” in the following alphabetical list. Clearly indicate how you choose your pivots and which part of the list you are rejecting at each stage.
Arc, Centre, Chord, Circle, Circumference, Diameter, Radius, Sector, Segment, Tangent
(Total 4 marks)
题目中文翻译
使用二分搜索算法尝试在以下字母列表中定位单词”Parallelogram”。清楚说明如何选择枢轴以及每个阶段拒绝列表的哪一部分。
Arc、Centre、Chord、Circle、Circumference、Diameter、Radius、Sector、Segment、Tangent
解答
解法一
思路
展开
为列表中的单词编号 至 ,每次使用 middle-right 位置作为枢轴。把目标单词与枢轴按字母顺序比较后,连同枢轴一起排除不可能包含目标的那一半;继续到候选区间为空为止。
答题过程
展开
Number the words from to . The first middle-right pivot is
which is Diameter. Since Parallelogram comes after Diameter alphabetically, reject positions to and retain positions to .
The next pivot is
which is Segment. Since Parallelogram comes before Segment, reject positions and and retain positions and .
The next pivot is
which is Sector. Since Parallelogram comes before Sector, reject position .
The only remaining word, at position , is Radius. This is not Parallelogram, so position is also rejected.
Therefore