Block swap algorithms: Difference between revisions
→Reversal algorithm: improve phrasing |
Tagishsimon (talk | contribs) m rm cat improve |
||
Line 16: | Line 16: | ||
==References== |
==References== |
||
{{reflist}} |
{{reflist}} |
||
{{Improve categories|date=July 2019}} |
|||
[[Category:Algorithms]] |
[[Category:Algorithms]] |
Revision as of 01:55, 19 July 2022
![]() | This article provides insufficient context for those unfamiliar with the subject.(July 2019) |
In computer algorithms, Block swap algorithms swap two regions of elements of an array. It is simple to swap two non-overlapping regions of an array of equal size. However, it is not simple to swap two non-overlapping regions of an array in-place that are next to each other, but are of unequal sizes (such swapping is equivalent to Array Rotation). Three algorithms are known to accomplish this: Bentley's Juggling (also known as Dolphin Algorithm [1]), Gries-Mills, and Reversal.[2] All three algorithms are linear time O(n), (see Time complexity).
Reversal algorithm
The reversal algorithm is the simplest to explain, using rotations. A rotation is an in-place reversal of array elements. This method swaps two elements of an array from outside in within a range. The rotation works for an even or odd number of array elements. The reversal algorithm uses three in-place rotations to accomplish an in-place block swap:
- Rotate region A
- Rotate region B
- Rotate region AB
Gries-Mills and Reversal algorithms perform better than Bentley's Juggling, because of their cache-friendly memory access pattern behavior.
The Reversal algorithm parallelizes well, because rotations can be split into sub-regions, which can be rotated independently of others.
References
- ^ D. Gries, H. Mills (1981), Swapping Sections
- ^ Jon Bentley, "Programming Pearls", pp. 13–15, 209-211.