Internal Sorting: Natural Two-way Merge Sort: Sorting by Merging

自然双向归并排序算法N详细介绍了如何使用两个内存区域对记录进行排序,每个区域可以容纳N个记录。该算法通过比较和传输记录,不断切换输出方向,最终实现有序排列。
摘要由CSDN通过智能技术生成

Algorithm N

Algorithm N (Natural two-way merge sort). Records R1,…,RN are sorted
using two areas of memory, each of which is capable of holding N records. For
convenience, we shall say that the records of the second area are RN+1, …, R2N,
although it is not really necessary that RN+1 be adjacent to RN. The initial
contents of RN+1, … , R2N are immaterial. After sorting is complete, the ‘keys
will be in order, K1 <= … <= KN.
N1. [Initialize.] Set s <– 0. (When s = 0, we will be transferring records from
the (R1,…, RN) area to the (RN+1, … , R2N) area; when s = 1, we will
be going the other way.)
N2. [Prepare for pass.] If s = 0, set i <– 1, j <– N, k <– N+1, I <– 2N; if
s = 1, set i <– N+1, j <– 2N, k <– 1, I <– N. (Variables i, j, k, I point to
the current positions in the “source files” being read and the “destination
files” being written.) Set d <– 1, f <– 1. (Variable d gives the current
direction of output; f is set to zero if future passes are necessary.)
N3. [Compare Ki:Kj.] If Ki > Kj, go to step N8. If i = j, set Rk <– Ri and
go to N13.
N4. [Transmit Ri.] (Steps N4-N7 are analogous to steps M3-M4 of Algo-
rithm M.) Set Rk <– Ri, k <– k+d.
N5. [Stepdown?] Increase i by 1. Then if Ki-1 <= Ki, go back to step N3.
N6. [Transmit Rj.] Set Rk <– Rj, k <– k+d.
N7. [Stepdown?] Decrease j by 1. Then if Kj+1 <= Kj, go back to step N6;
otherwise go to step N12.
N8. [Transmit Rj.] (Steps N8-N11 are dual to steps N4-N7.) Set Rk <– Rj,
k <– k+d.
N9. [Stepdown?] Decrease j by 1. Then if Kj+1 <= Kj, go back to step N3.
N10. [Transmit Ri] Set Rk <– Ri, k <– k+d.
N11. [Stepdown?] Increase i by 1. Then if Ki-1 <= Ki, go back to step N10.
N12. [Switch sides.] Set f <– 0, d <– -d, and interchange k <–> I. Return to
step N3.
N13. [Switch areas.] If = 0, set s <– 1-s and return to N2. Otherwise sorting
is complete; if s = 0, set (R1,…, RN) <– (RN+1, … , R2N). (

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值