www.heteroclinic.net
In Place Array Segments Swap 1 201701
In a given array of integers, there are two arbitrary segments. We want to exchange their positions, such that regard each segment itself only, the elements' relative positions are not changed. The the order of the segments are changed. We can give two examples to illustrate the requirement. We also want the array operation to be in-place, namely we don't try to allocate more resource so we can keep the program low memory profile.
|
||
In Place Array Segments Swap 2 201701
When the two segments' sizes are equal, it is a trivia case.
|
||
In Place Array Segments Swap 3 201701
While dealing with the segments without equal length, we can formalize the algorithm as the following. We also restrain our design by not using recursion, since recursion will also increase memory profile. It may also be easy to debug in while loop than recursion when we have many parameters to pass among recursion stack.
|
||
In Place Array Segments Swap 4 201701
This method may be used for memory manipulation in a very stressed situation. We also use it for the design of an in-place merge sort implementation. We attach the implementation and test cases.
|