Ratio based stable in-place merging
Pok-Son Kim1and Arne Kutzner2
1Kookmin University, Department of Mathematics, Seoul 136-702, Rep. of Korea
2Seokyeong University, Department of Computer Science, Seoul 136-704, Rep. of
Abstract. We investigate the problem of stable in-place merging from
a ratio k =
sequences with m ≤ n . We introduce a novel algorithm for this problem
that is asymptotically optimal regarding the number of assignments as
well as comparisons. Our algorithm uses knowledge about the ratio of
the input sizes to gain optimality and does not stay in the tradition of
Mannila and Ukkonen’s work  in contrast to all other stable in-place
merging algorithms proposed so far. It has a simple modular structure
and does not demand the additional extraction of a movement imitation
buffer as needed by its competitors. For its core components we give
concrete implementations in form of Pseudo Code. Using benchmarking
we prove that our algorithm performs almost always better than its direct
competitor proposed in .
As additional sub-result we show that stable in-place merging is a quite
simple problem for every ratio k ≥√m by proving that there exists a
primitive algorithm that is asymptotically optimal for such ratios.
mbased point of view where m,n are the sizes of the input
Merging denotes the operation of rearranging the elements of two adjacent sorted
sequences of size m and n, so that the result forms one sorted sequence of m+n
elements. An algorithm merges two sequences in place when it relies on a fixed
amount of extra space. It is regarded as stable, if it preserves the initial ordering
of elements with equal value.
There are two significant lower bounds for merging. The lower bound for the
number of assignments is m + n because every element of the input sequences
can change its position in the sorted output. As shown e.g. in Knuth  the
lower bound for the number of comparisons is Ω(mlog(n
A merging algorithm is called asymptotically fully optimal if it is asymptotically
optimal regarding the number of comparisons as well as assignments.
We will inspect the merging problem on the foundation of a ratio based approach.
In the following k will always denote the ratio k =
sequences. The lower bounds for merging can be expressed on the foundation of
such a ratio as well. We get Ω(mlog(k + 1)) as lower bound for the number of
comparisons and m · (k + 1) as lower bound for the number of assignments.
m+ 1)), where m ≤ n.
mof the sizes of the input
In the first part of this paper we will show that there is a simple asymptot-
ically fully optimal stable in-place merging algorithm for every ratio k ≥√m.
Afterward we will introduce a novel stable in-place merging algorithm that is
asymptotically fully optimal for any ratio k. The new algorithm has a modular
structure and does not rely on the techniques described by Mannila and Ukko-
nen  in contrast to all other works ([10,4,2,6]) known to us. Instead it exploits
knowledge about the ratio of the input sizes to achieve optimality. In its core
our algorithm consists of two separated operations named “Block rearrangement”
and “Local merges”. The separation allowed the omitting of the extraction of an
additional movement imitation buffer as e.g. necessary in . For core parts of
the new algorithm we will give an implementation in Pseudo-Code. Some bench-
marks will show that it performs better than its competitor proposed in  for
a wide range of inputs.
A first conceptual description of a stable asymptotically fully optimal in-
place merging algorithm can be found in the work of Symvonis . Further work
was done by Geffert et al.  and Chen  where Chen presented a simplified
variant of Geffert et al’s algorithm. All three publications delivered neither an
implementation in Pseudo-Code nor benchmarks. Recently Kim and Kutzner
 published a further algorithm together with benchmarks. These benchmarks
proved that stable asymptotically fully optimal in-place merging algorithms are
competitive and don’t have to be viewed as theoretical models merely.
2A simple asymptotically optimal algorithm for k ≥√m
Hwang and Lin
u,v with |u| ≤ |v|
m = |u|,n = |v|
m(t + 1) + n/2t
t = ?log(n/m)?
(1) - ext. buffer
(2) - m rotat.
2m + n
n + m2+ m
|u| + |v| + gcd(|u|,|v|)
≤ 2(|u| + |v|)
u,v with |u| = |v|
u, x (searched element)
u, let m = |u|
Table 1. Complexity of the Toolbox-Algorithms
?log|u|? + 1
|u| − 1
+ (m − 1)
We now introduce some notations that will be used throughout the paper.
Let u and v be two ascending sorted sequences. We define u ≤ v (u < v) iff
x ≤ y (x < y) for all elements x ∈ u and for all elements y ∈ v. |u| denotes the
size of the sequence u. Unless stated otherwise, m and n (m ≤ n) are the sizes
of two input sequences u and v respectively. δ always denotes some block-size
with δ ≤ m.
Tab. 1 contains the complexity regarding comparisons and assignments for six
elementary algorithms that we will use throughout this paper. Brief descriptions
of these algorithms except for “Minimum Search” can be found in . In the case
of “Minimum Search” we assume that u is unsorted, therefore a linear search is
First we will now show that there is a simple stable merging algorithm called
Block-Rotation-Merge that is asymptotically fully optimal for any ratio
k ≥√m. Afterward we will prove that there is a relation between the number of
different elements in the shorter input sequence u and the number of assignments
performed by the rotation based variant of Hwang and Lin’s algorithm .
Algorithm 1: Block-Rotation-Merge (u,v,δ)
1. We split the sequence u into blocks u1u2...u?m
of ui (i = 1,···,?m
3. We locally merge all pairs uiviusing?m
The steps 2 and 3 are interlaced as follows: After creating a new pair uivi(i =
Lemma 1. Block-Rotation-Merge performs
assignments at most if we use the optimal algorithm from Dudzinski and Dydek
 for all block-rotations .
δ?so that all sections u2to
δ?are of equal size δ and u1is of size mmodδ. Let xibe the last element
into sections v1v2...v?m
of Hwang and Lin’s algorithm ().
?). Using binary searches we compute a splitting of v
δ?is reorganized to
?calls of the rotation based variant
δ?so that vi< xi≤ vi+1(i = 1,···,?m
?− 1 many rotations.
?) as part of the second step we immediately locally merge this pair as
described in step 3.
2·δ+ 2n + 6m + m · δ many
Proof. For the first rotation from u1u2···u?m
gorithm performs |u2| + ··· + |u?m
signments. The second rotation from u2u3···u?m
the last rotation from u?m
the algorithm requires |u?m
merges. Altogether the algorithm performs δ · ((m
n + n + 6m + m · δ =m2
assignments at most.
δ?v1 to u1v1u2···u?m
δ?| + |v1| + gcd(|u2| + ··· + |u?m
δ?|, |v1|) as-
δ?|, |v2|) assignments, and so on. For
δ?| + |v?m
δ?−1| + gcd(|u?m
δ(3δ+3δ+δ2) = 6m+m·δ assignments are required for the local
δ− 1) + (m
δ− 2) + ··· + 1) +
2·δ+ 2 · n + 6m + m · δ
2+ 2 · n + 6m + m · δ ≤m2
Lemma 2. Block-Rotation-Merge is asymptotically optimal regarding the
number of comparisons.
Corollary 1. If we assume a block-size of ?√m? then Block-Rotation-Merge
is asymptotically fully optimal for all k ≥√m.
So, for k ≥√m there is a quite primitive asymptotically fully optimal stable
in-place merging algorithm. In the context of complexity deliberations in the
next section we will rely on the following Lemma.
Lemma 3. Let λ be the number of different elements in u. Then the number of
assignments performed by the rotation based variant of Hwang and Lin’s algo-
rithm is O(λ · m + n) = O((λ + k) · m).
Proof. Let u = u1u2...uλ, where every ui(i = 1,···,λ) is a maximally sized
section of equal elements. We split v into sections v1v2...vλvλ+1so that we get
vi< ui≤ vi+1(i = 1,···,λ). (Some vican be empty.) We assume that Hwang
and Lin’s algorithm already merged a couple of section and comes to the first
elements of the section ui(i = 1,···,λ). The algorithm now computes the section
viand moves it in front of uiusing one rotation of the form ···ui...uλvi··· to
···viui...uλ···. This requires |ui|+···+|uλ|+|vi|+gcd(|ui|+···+|uλ|, |vi|) ≤
2(m+|vi|) many assignments. Afterward the algorithm continues with the second
element in ui. Obviously there is nothing to move at this stage because all
elements in uiare equal and the smaller elements from v were already moved
in the step before. Because we have only λ different sections we proved our
Corollary 2. Hwang and Lin’s algorithm is fully asymptotically optimal if we
have either k ≥ m or k ≥ λ where λ is the number of different elements in the
shorter input sequence u .
3Novel asymptotically optimal stable in-place merging
We will now propose a novel stable in-place merging algorithm called Stable-
Optimal-Block-Merge that is fully asymptotically optimal for any ratio. No-
table properties of our algorithm are: It does not rely on the block management
techniques described in Mannila and Ukonnen’s work  in contrast to all other
such algorithms proposed so far. It degenerates to the simple Block-Rotation-
Merge algorithm for roughly k ≥√m/2 . The internal buffer for local merges
and the movement imitation buffer share a common buffer area. The two opera-
tions “block rearrangement” and “local merges” stay separated and communicate
using a common block distribution storage. There is no lower bound regarding
the size of the shorter input sequence.
Algorithm 2: Stable-Optimal-Block-Merge
Step 1: Block distribution storage assignment
Let δ = ?√m? be our block-size. We split the input sequence u into u = s1ts2u?
so that s1and s2are two sequences of size ?m/δ? + ?n/δ? and t is a sequence
of maximal size with elements equal to the last element of s1. We assume that
there are enough elements to get a nonempty u?and call s1together with s2our
block distribution storage (in the following shortened to bd-storage).
Step 2: Buffer extraction
(all elements in this area are distinct)
elements originating from u
block distribution storage
Fig.1. Segmentation after the buffer extraction
In front of the remaining sequence u?we extract an ascending sorted buffer b of
size δ so that all pairs of elements inside b are distinct. Simple techniques how to
do so are proposed e.g. in  or . Once more we assume that there are enough
elements to do so. Now let w be the remaining right part of u?after the buffer
The segmentation of our input sequences after the buffer extraction is shown in
Step 3: Block rearrangement
> (elements exchanged)
(here used as movement imitation buffer)
first and second section of the block distribution storage
Fig.2. Graphical remarks to the block rearrangement process
We logically split the sequence wv into blocks of equal size δ as shown in Fig.
2 (a). The two blocks w1and v?n
the following we call every block originating from w a w-block and every block
δ?+1are undersized and can even be empty. In