共計 1651 個字符,預計需要花費 5 分鐘才能閱讀完成。
本篇內容主要講解“PostgreSQL 中 REPLACEMENT SELECTION SORT 使用與哪種情況”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓丸趣 TV 小編來帶大家學習“PostgreSQL 中 REPLACEMENT SELECTION SORT 使用與哪種情況”吧!
REPLACEMENT SELECTION SORT, 適用于內存較小的情況 (在高德納提出該算法的年代, 內存比起現在少了 N 個數量級).
該算法在 PG 9.x 或以下版本在使用, 在高版本已被廢棄. 其算法實現如下:
Let m be the number of records to be sorted which can be kept in the main memory. Imagine that these memory locations are registers and assume we can mark them as“on”or“off”. Replacement selection can overlap reading, sorting and writing.
Step1: The m registers are filled with records from the input to be sorted.
Step2: All registers are put into the “on” state.
Step3: Select the register which has the smallest of all “on” registers.
Step4: Transfer the contents of the selected register to the output (call it as key Y).
Step5: Replace the contents of the selected register by the next input record.
If new record key Y
Go to step 3
If new record key = =Y
Go to step 4
If new record key Y
Go to step 6
Step6: Turn the selected key register “off”.
If all registers are now “off” We have completed a sorted substring(run).
We start a new substring group and go to step 2.
Else
Go to step 3
下面是該算法的一個例子:
Example: Suppose number of registers is 3 and input keys are 5, 2, 9, 7, 0, 8, 1, 6, 3, 4 ...
Registers Output
5 2 9 2
5 7 9 5
*0 7 9 7
*0 8 9 8
*0*1 9 9
*0*1*6
0 1 6 0
3 1 6 1
3 4 6 3
- 4 6 4
- - 6 6
- - -
相關的數據結構: 兩個數組, 一個存儲讀取的實際數據, 一個標記數組; 一個外存, 用于存儲已完成排序的一個 run.
如標記數組全為 F(非活動狀態), 則產生一個 run, 周而復始, 直至所有輸入被耗盡為止.
通過這個方法, 可以生成 N 個 runs(每個 run 對應的是已完成排序的部分數據).
CS 351 -Data Organization and Management (Section: 02)
External_sorting
replacement-selection-sort vs selection-sort
到此,相信大家對“PostgreSQL 中 REPLACEMENT SELECTION SORT 使用與哪種情況”有了更深的了解,不妨來實際操作一番吧!這里是丸趣 TV 網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!