Stable separation for two classes of elements in an array
Consider the following problem.
We are given an array of eleme开发者_StackOverflow中文版nts belonging to one two classes: either red or blue. We have to rearrange the elements of the array so that all blue elements come first (and all red elements follow). The rearrangement must be done is stable fashion, meaning that the relative order of blue elements must be preserved (same for red ones).
Is there a clever algorithm that would perform the above rearrangement in-place?
A non-in place solution is, of course, straightforward.
An obvious in-place solution would be to apply any stable sorting algorithm to the array. However, using a full-fledged sorting algorithm on an array intuitively feels like an overkill, especially taking into account the fact that we are only dealing with two classes of elements.
Any ideas greatly appreciated.
It is possible to do it in O(n) time and O(1) space apparently. The paper Stable minimum space partitioning in linear time by Jyrki Katajainen, and Tomi Pasanen claims to be able to do it.
Google for stable 0-1 sort.
I don't know O(n) algorithm but here's a simple algorithm with O(n*log(n)) complexity in the worst case. Maybe it will be useful.
Cut off the zeroes from beginning and ones from the end, they are already on their places. Now the array looks like a sequence of ones followed by sequence of zeroes followed by sequence of ones and so on, like this: 1010101010
Exchange the first sequence of ones with the first sequence of zeroes, the third sequence of ones with the third sequence of zeroes and so on. It will become: 0110011001
The amount of sequences became about twice less. Repeat the above procedure until the array is sorted.
To exchange two neighbour sequences reverse the first one, then the second one, and then reverse them both.
This is called Stable Partitioning in C++ and there is a standard algorithm for this: std::stable_partition.
The SGI implementation has an adaptive behavior depending on the amount of memory available:
- it runs in O(N) if it's possible to allocate O(N) space
- it runs in O(N log N) swaps in place
I do wonder if the latter in place solution uses a stable sorting algorithm behind the scenes.
精彩评论