7. Linear-Time Selection

問題:We aim to find the i-th smallest element from a set A containing n elements, 1 <= i <= n.

思路: The general strategy is to find a pivot element x in A such that a constant fractional number of elements in A can be discarded for further consideration.

The key to the proposed algorithm is how to find such a pivot element x within each iteration efficiently!

Linear-Time Selection

Step 1. Divide the n elements into n/5 groups of 5 elements each (each group contains exactly 5 elements except the last group which may contain less than 5 elements).

Step 2. Find the median of each group by any sorting method.
(If the number of elements in the last group is even, take either one of the two medians).
Consequently,a median sequence consisting of group medians is formed, which contains n/5 elements exactly with each group having its median there.

Step 3. Find the median x from the median sequence of n/5 “group median” elements, using the linear selection algorithm recursively, where element x is the pivot element that will be used to partition the set A into three disjoint subsets R1, R2 and R3.

Step 4. Partition the n-element set A into three disjoint subsets R1, R2 and R3, using the found x (as the pivot element), i.e., the set A is partitioned into three disjoints subsets R1, R2, and R3 . R2 = { x}


Analysis of the Linear Selection Algorithm

Step 1. Partition the n elements into n/5 groups, it take linear time to scan the n elements, i.e., O(n) time

Step 2. Perform sorting within each group, thus it takes constant time to sort the 5-element in each group. In total, this step takes n/5 * O(1) = O(n) time, where each group contains no more than 5 elements, its sorting time is O(1).

Step 3. Find the median x of the median sequence of n/5 elements, which takes T (n/5) time. Notice that the running time of the selection algorithm is applicable for any parameter i with 1 <= i <= |A|.

Step 4. Use the value of x to partition the set A into three disjoint subsets R1, R2 and R3, it takes O(n) time

Step 5. If |R1| < i <= |R1|+|R2|, done.
Otherwise, call the selection algorithm either on R1 or R3, not both of them, it takes max{T (|R1|), T (|R3|)}. Fortunately we can show that both |R1| and |R3| are less than 7n/10 + 6, i.e., a fraction length of the original length n. Thus, this step takes T (7n/10 + 6) time.

證明:這里有n個(gè)元素漂辐,有n/5個(gè)組徒像。通過一系列比較,可以得到median x 至少要大于3((n/5)/2 - 2)個(gè)數(shù)。減2是因?yàn)橐獪p去它自己本身和最后一個(gè)少于五個(gè)元素的數(shù)組摘仅。

所以the running time of the linear selection
algorithm: T(n) < T(n/5) + T(n - 3*((n/5) / 2 - 2)) + O(n) (n > 140)
or T(n) = O(1) ( n <= 140)

To prove the time complexity of the linear selection
algorithm is O(n), I used the Substitution method

Demenstration
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌尉剩,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件毅臊,死亡現(xiàn)場(chǎng)離奇詭異理茎,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)管嬉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門皂林,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人蚯撩,你說我怎么就攤上這事式撼。” “怎么了求厕?”我有些...
    開封第一講書人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)扰楼。 經(jīng)常有香客問我呀癣,道長(zhǎng),這世上最難降的妖魔是什么弦赖? 我笑而不...
    開封第一講書人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任项栏,我火速辦了婚禮,結(jié)果婚禮上蹬竖,老公的妹妹穿的比我還像新娘沼沈。我一直安慰自己,他們只是感情好币厕,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開白布列另。 她就那樣靜靜地躺著,像睡著了一般旦装。 火紅的嫁衣襯著肌膚如雪页衙。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,718評(píng)論 1 305
  • 那天阴绢,我揣著相機(jī)與錄音店乐,去河邊找鬼。 笑死呻袭,一個(gè)胖子當(dāng)著我的面吹牛眨八,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播左电,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼廉侧,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼页响!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起伏穆,我...
    開封第一講書人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤拘泞,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后枕扫,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體陪腌,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年烟瞧,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了诗鸭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡参滴,死狀恐怖强岸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情砾赔,我是刑警寧澤蝌箍,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站暴心,受9級(jí)特大地震影響妓盲,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜专普,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一悯衬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧檀夹,春花似錦筋粗、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至偶摔,卻和暖如春暇唾,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背辰斋。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工策州, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人宫仗。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓够挂,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親藕夫。 傳聞我的和親對(duì)象是個(gè)殘疾皇子孽糖,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容