你走进一个有 200 名儿童的体育馆。现在是 9 月 8 日,所以你渴望找到第 98 个最矮的孩子。你知道你可以把它们从最短到最高排列起来,但这需要很长时间。“我知道”,你会想,“我可以使用 QUICKSELECT!”
你走出人群,闭上眼睛,伸出手指,转了三圈。当你睁开眼睛时,你正直指其中一个孩子,彼得·皮沃特。你说:“快!比彼得矮的人都站在这里。比彼得高的人都站到那边。如果你和彼得一样高,你可以进入任何一组。”
孩子们四处走动,很快他们就站在了两组中。你数了数,发现矮个组有 120 个孩子,高个组有 79 个孩子。你知道第 98 个最矮的孩子一定是矮个子组,所以你告诉彼得和 79 个个子高的孩子坐在看台上。
你再次闭上眼睛,伸出手指,转了三圈。当你睁开眼睛时,你正直指着彼得的妹妹宝拉·皮沃特。你说:“快点!那些还站着的人。如果你比宝拉矮,就站到这里来。如果你比宝拉高,就站在那里。如果你和宝拉一样高,你可以进入任一组。”
孩子们四处走动,很快他们就站在了两组中。你数了数,发现矮个组有 59 个孩子,高个组有 60 个孩子。你知道第 98 个最矮的孩子必须在较高的一组,所以你告诉宝拉和 59 个矮个的孩子坐在看台上。
You again close your eyes, stick out your finger, and spin around three times. When you open your eyes, you are pointing directly at Paula's cousin, Prudence Pivot. You say, "Quickly! Those of you who are still standing. If you're shorter than Prudence, come stand over here. If you're taller than Prudence, go stand over there. If you're the same height, you can go into either group."
The children shuffle around, and soon they are standing in the two groups. You count and find 37 children in the shorter group, and 22 children in the taller group. You know that Paula and 59 other shorter children are sitting on the bleachers. Along with the 37 shorter children still standing, you know that a total of 97 children are shorter than Prudence. Therefore, Prudence is the 98th shortest child!
Quickselect FTW!