本篇内容主要讲解“PostgreSQL中REPLACEMENT SELECTION SORT使用与哪种情况”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“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使用与哪种情况”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!