Page 1
Ratio based stable inplace merging
PokSon Kim1and Arne Kutzner2
1Kookmin University, Department of Mathematics, Seoul 136702, Rep. of Korea
pskim@kookmin.ac.kr
2Seokyeong University, Department of Computer Science, Seoul 136704, Rep. of
Korea kutzner@skuniv.ac.kr
Abstract. We investigate the problem of stable inplace 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 [8] in contrast to all other stable inplace
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 [6].
As additional subresult we show that stable inplace 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.
n
mbased point of view where m,n are the sizes of the input
1 Introduction
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 [7] 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.
n
mof the sizes of the input
Page 2
In the first part of this paper we will show that there is a simple asymptot
ically fully optimal stable inplace merging algorithm for every ratio k ≥√m.
Afterward we will introduce a novel stable inplace 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 [8] 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 [6]. For core parts of
the new algorithm we will give an implementation in PseudoCode. Some bench
marks will show that it performs better than its competitor proposed in [6] 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 [10]. Further work
was done by Geffert et al. [4] and Chen [2] where Chen presented a simplified
variant of Geffert et al’s algorithm. All three publications delivered neither an
implementation in PseudoCode nor benchmarks. Recently Kim and Kutzner
[6] published a further algorithm together with benchmarks. These benchmarks
proved that stable asymptotically fully optimal inplace merging algorithms are
competitive and don’t have to be viewed as theoretical models merely.
2A simple asymptotically optimal algorithm for k ≥√m
Algorithm
Hwang and Lin
Arguments
u,v with u ≤ v
let
m = u,n = v
Comparisons
m(t + 1) + n/2t
where
t = ?log(n/m)?
Assignments
(1)  ext. buffer
(2)  m rotat.
Block Swapping
Block Rotation
2m + n
n + m2+ m
3u
u + v + gcd(u,v)
≤ 2(u + v)


m(m+1)
2
u,v with u = v
u,v


Binary Search
Minimum Search
Insertion Sort
u, x (searched element)
u
u, let m = u
Table 1. Complexity of the ToolboxAlgorithms
?logu? + 1
u − 1
m(m−1)
2
+ (m − 1)
− 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
Page 3
of two input sequences u and v respectively. δ always denotes some blocksize
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 [6]. In the case
of “Minimum Search” we assume that u is unsorted, therefore a linear search is
necessary.
First we will now show that there is a simple stable merging algorithm called
BlockRotationMerge 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 [5].
Algorithm 1: BlockRotationMerge (u,v,δ)
1. We split the sequence u into blocks u1u2...u?m
u?m
of ui (i = 1,···,?m
2. u1u2...u?m
u1v1u2v2...u?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 =
1,···,?m
Lemma 1. BlockRotationMerge performs
assignments at most if we use the optimal algorithm from Dudzinski and Dydek
[3] for all blockrotations .
δ?so that all sections u2to
δ?are of equal size δ and u1is of size mmodδ. Let xibe the last element
δ
into sections v1v2...v?m
δ?v1v2...v?m
δ?v?m
of Hwang and Lin’s algorithm ([5]).
?). 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
δ?using?m
δ
δ
?− 1).
?− 1 many rotations.
δ
?) as part of the second step we immediately locally merge this pair as
described in step 3.
m2
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
u3+···+u?m
the last rotation from u?m
the algorithm requires u?m
Additionallym
merges. Altogether the algorithm performs δ · ((m
n + n + 6m + m · δ =m2
assignments at most.
δ?v1 to u1v1u2···u?m
δ?the al
δ? + v1 + gcd(u2 + ··· + u?m
δ?, v1) as
δ?v2to u2v2u3···u?m
δ?requires
δ?+v2+gcd(u3+···+u?m
δ?−1u?m
δ?, v2) assignments, and so on. For
δ?−1v?m
δ?, v?m
δ?v?m
δ?to u?m
δ?−1v?m
δ?−1u?m
δ?v?m
δ?
δ? + v?m
δ?−1 + gcd(u?m
δ?−1) assignments.
δ(3δ+3δ+δ2) = 6m+m·δ assignments are required for the local
δ− 1) + (m
δ− 2) + ··· + 1) +
2·δ+ 2 · n + 6m + m · δ
2·δ−m
2+ 2 · n + 6m + m · δ ≤m2
? ?
Lemma 2. BlockRotationMerge is asymptotically optimal regarding the
number of comparisons.
Page 4
Corollary 1. If we assume a blocksize of ?√m? then BlockRotationMerge
is asymptotically fully optimal for all k ≥√m.
So, for k ≥√m there is a quite primitive asymptotically fully optimal stable
inplace 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
conjecture.
? ?
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 inplace merging
algorithm
We will now propose a novel stable inplace merging algorithm called Stable
OptimalBlockMerge 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 [8] in contrast to all other
such algorithms proposed so far. It degenerates to the simple BlockRotation
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: StableOptimalBlockMerge
Step 1: Block distribution storage assignment
Let δ = ?√m? be our blocksize. 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
Page 5
there are enough elements to get a nonempty u?and call s1together with s2our
block distribution storage (in the following shortened to bdstorage).
Step 2: Buffer extraction
w
v
t
s1
(all elements in this area are distinct)
s2
b
elements originating from u
block distribution storage
buffer
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 [9] or [6]. 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
extraction.
The segmentation of our input sequences after the buffer extraction is shown in
Fig. 1.
Step 3: Block rearrangement
wr−1
wr
v1
v?n
δ?
w1
w2
buffer area
≤
(a)
(b)
wblock
vblock
<
vblock
wblock
wblock
vblock
> (elements exchanged)
vblock
(here used as movement imitation buffer)
first and second section of the block distribution storage
(c)
v?n
δ?+1
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 wblock and every block
δ?+1are undersized and can even be empty. In