ADA 4.2: Selection Problem 選擇問題 (求第k名)
Textbook Chapter 9.3 - Selection in worst case linear time
What is Selection Problem?
取一有 個 integers 的
求輸出 裡第 大的數字
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Solution 1: Sorting
藉由將陣列 重新由小排到大,要第 大就直接取第 位置的值
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
但因為用到排序(sort),所以其時間複雜度落在
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
當 , sorting 的時間複雜度就會越差越多
如何將時間複雜度再往下調呢?
Solution 2: Divide-and-Conquer
想法
- 設定一個基準點(),將原本的陣列依基準點拆成兩半
- 若 ,則代表 落在 裡的第 位置上
- 若 ,則代表 落在 裡的第 位置上
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
我們會希望基準點()會盡量在中間,因為如果基準點一邊個數太大,若 剛好都落在較大的一邊,則最差情況你要拆 遍才能找到你要的數值
如何找尋中位數?
- 尋找中位數也是另一個 Selection Problem,只是 為中位數
Step 1: Five Men Per Group
- 將 n 個個數的 array 每5個一組,而會分成總共 組,每組因為數量為 constant (5個),強制sorting所花費的時間為 constant
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
上圖為取中間值(黃點)而所做 sorting 動作,箭頭代表序列由小變大
- 從 組的所有中間值(medians)在做排序取得中間值(median)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
上圖將所有medians在做排序在取其中間值(紅點)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
在 medians 取得 median 時,若 medians 個數為偶數,通常會取較小的那個值當作 MoM
Reference: Chapter 9.3 p220.第三點
Step 4: Partition via MoM
- 在取得MoM後,將此數作為切割點(pivot),在 的左邊為小於 元素的集合 ,右邊則為大於 元素的集合
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Step 5: Recursion
接著我們就要判斷 位於哪個地方,依照上圖會有三種情況
- 落於 裡面(其 index 一樣落在 的第 位置上)
- 落於 上(index 應為 )
- 落於 裡面(其 index 會落在 的第 位置上)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
這裡的recursion有牽涉兩個地方
- 找尋
- 落於 或者 ,就要再做一次遞迴拆解一次(直到序列夠小可以直接找到 值)
Divide-and-Conquer for Selection Problem
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
依序拆解各個時間複雜度
- base case:
- divide groups with size 5:
- median from group i:
- MoM:
- case divide:
- case 1:
- case 2:
- case 3:
我們希望 、 的數量盡量不要差距太大(不然 recursive 的次數就越多)
在做 recursion 時可以每次去掉一些元素
以 在 、 的情況每次可剔除
下回要 recursion 的數量就變為
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Time Complexity
becomes…
applying Master Method case 3
applying Sustitution Method
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →