block swaping 原地归并排序的基础(in-place parallel merge sort)

本文探讨了如何在原地进行两个大小不等的子数组交换,即Block Exchange(Block Swap)。介绍了三种算法:Juggling、Gries-Mills和Reversal算法,并通过基准测试比较了它们的性能。Juggling算法使用额外存储空间,Gries-Mills算法交换最大可用的相等大小的块,而Reversal算法则利用旋转操作。结果显示,Gries-Mills算法在性能上领先,其次是Reversal算法,而Juggling算法性能较差,主要是因为其内存访问模式不友好。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Swapping two elements within an array quickly is simple, requiring a single temporary storage element.

template < class Item >
inline void exchange( Item& A, Item& B )
{
    Item t = A;
    A = B;
    B = t;
}

Swapping two non-overlapping subarrays of the same size is also simple.

 
Figure 1.

In Figure 1, the red subarray, containing two elements, is swapped with the blue subarray, also containing two elements. The order of elements within each subarray is preserved. The two subarrays can have zero or more elements separating them. The following code implements an equal-sized non-overlapping Block Swap:

template< class _Type >
inline void swap( _Type* x, unsigned a, unsigned b, unsigned m )
{
    for( ; m > 0; m-- )
        exchange( x[ a++ ], x[ b++ ] );
}

If the two subarrays are overlapping, an issue arises.

 
Figure 2.

In Figure 2, the first subarray consists of elements 0 and 1, and the second consists of elements 1 and 2. Both subarrays contain element 1. The result should be as if we made copies of the two subarrays, swapped them, and then merged the result, as illustrated in Figure 3:

 
Figure 3.

In Figure 3, the first row shows the original array with two overlapping subarrays; the second row separates the two subarrays; the third row swaps the two subarrays; and the fourth row attempts to combine the two results, leading to an ambiguity due to two possible results in the overlapping middle element.

We could define the overlapping swap operation to favor the first or the second subarray, but in either case, some array element is lost, which is be problematic because swapping is expected to preserve array elements.

Swapping two non-overlapping subarrays of any size, equal or not, is called Block Exchange, Block Swap, or Section Swap.

 
Figure 4.

In Figure 4, in the left column, the red subarray of one element is swapped with the blue subarray of two elements, with a one separating element in grey. In the right column, the red subarray of two elements is swapped with the blue element of three elements, with no elements of separation. Note that the order of elements within each subarray is preserved.

Several algorithms have been developed to solve this problem in linear time, in-place. Some of these algorithms are easily parallelized well, while others are not. In this article, I explain these sequential algorithms and measure their performance. In upcoming articles, the algorithms will be generalized and parallelized. Fin

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值