© 2000 by British Computer Society
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
In situ, Stable Merging by Way of the Perfect Shuffle
1 Department of Computer Science, University of Victoria, PO Box 3055, Victoria, British Columbia, V8W 3P6, Canada Email: jellis@csr.uvic.ca
We introduce a novel approach to the classical problem of the in situ, stable merging of lists, where in situ means the use of no more than $O(\log^2 n)$ bits of extra memory for lists of size $n$. Our algorithm, Shufflemerge, reduces the merging problem to the problem of realizing the perfect shuffle permutation, that is, the exact interleaving of two, equal length lists. The algorithm is recursive and so uses a logarithmic number of variables. Thus it uses more than absolutely minimum storage, i.e. $O(\log n)$ bits. A simple method of realizing the perfect shuffle uses one extra bit per element, and so is not in situ. We show that the perfect shuffle can be attained using absolutely minimum storage and in linear time, at the expense of doubling the number of moves, relative to the simple method. We note that there is a worst case for Shufflemerge requiring time $\Omega(n \log n)$, where $n$ is the sum of the lengths of the input lists. We also present an analysis of a variant of Shufflemerge which uses a generalized shuffle and which has a provable average case time complexity of $O(n\log \log m)$, where $m$ is the length of the shortest input list. It is unlikely that the generalized shuffle can be achieved in situ. Linear time, in situ, stable merging has previously been demonstrated. We present experimental evidence indicating that Shufflemerge, although almost certainly not asymptotically linear, might be of value in practice. The relative simplicity of the basic method, particularly with respect to stability, also recommends it.
Received 7 May, 1998. Revised 1 November, 1999.