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 paralleliz

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

被折叠的 条评论
为什么被折叠?



