In the comparison model the only operations allowed on input elements are comparisons and moves to empty cells of memory. We prove the existence of an algorithm that, for any set of s ≤n sorted sequences containing a total of n elements, computes the whole sorted sequence using O(nlogs) comparisons, O(n) data moves and O(1) auxiliary cells of memory besides the ones necessary for the n input elements. The best known algorithms with these same bounds are limited to the particular case s= O(1). From a more intuitive point of view, our result shows that it is possible to pass from merging to sorting in a seamless fashion, without losing the optimality with respect to any of the three main complexity measures of the comparison model. Our main statement has an implication in the field of adaptive sorting algorithms and improves [Franceschini and Geffert, Journal of the ACM, 52], showing that it is possible to exploit some form of pre-sortedness to lower the number of comparisons while still maintaining the optimality for space and data moves. More precisely, let us denote with OptM(X) the cost for sorting a sequence X with an algorithm that is optimal with respect to a pre-sortedness measure M. To the best of our knowledge, so far, for any pre-sortedness measure M, no full-optimal adaptive sorting algorithms were known (see [Estivill-Castro and Wood, ACM Comp. Surveys, 24], page 472). The best that could be obtained were algorithms sorting a sequence X using O(1) space, O(OptM(X)) comparisons and O(OptM(X)) moves. Hence, the move complexity seemed bound to be a function of M(X) (as for the comparison complexity). We prove that there exists a pre-sortedness measure for which that is false: the pre-sortedness measure Runs, defined as the number of ascending contiguous subsequences in a sequence. That follows directly from our main statement, since ${Opt}_{M}(X)=O(\left\vert{X}\right\vert \log Runs(X))$
[1]
J. IAN MUNRO,et al.
An Implicit Data Structure Supporting Insertion, Deletion, and Search in O(log² n) Time
,
1986,
J. Comput. Syst. Sci..
[2]
Derick Wood,et al.
A survey of adaptive sorting algorithms
,
1992,
CSUR.
[3]
Jyrki Katajainen,et al.
Asymptotically efficient in-place merging
,
2000,
Theor. Comput. Sci..
[4]
Ronald L. Rivest,et al.
Introduction to Algorithms
,
1990
.
[5]
Luis Trabb Pardo.
Stable Sorting and Merging with Optimal Space and Time Bounds
,
1977,
SIAM J. Comput..
[6]
Clifford Stein,et al.
Introduction to Algorithms, 2nd edition.
,
2001
.
[7]
Jyrki Katajainen,et al.
In-Place Sorting with Fewer Moves
,
1999,
Inf. Process. Lett..
[8]
Mark H. Overmars,et al.
A balanced search tree with O(1) worst-case update time
,
1988,
Acta Informatica.
[9]
Antonios Symvonis,et al.
Optimal Stable Merging
,
1995,
Comput. J..
[10]
Michael A. Langston,et al.
Practical in-place merging
,
1987,
CACM.
[11]
Michael A. Langston,et al.
Stable set and multiset operations in optimal time and space
,
1988,
PODS '88.
[12]
Arne Andersson,et al.
Comparison-Efficient And Write-Optimal Searching and Sorting
,
1991,
ISA.
[13]
Jingchao Chen.
Optimizing stable in-place merging
,
2003,
Theor. Comput. Sci..
[14]
Rudolf Fleischer.
A Simple Balanced Search Tree with O(1) Worst-Case Update Time
,
1993,
ISAAC.
[15]
Edward C. Horvath,et al.
Stable Sorting in Asymptotically Optimal Time and Extra Space
,
1978,
JACM.
[16]
Gianni Franceschini,et al.
An in-place sorting with O(nlog n) comparisons and O(n) moves
,
2003,
JACM.
[17]
Jeffrey S. Salowe,et al.
Simplified Stable Merging Tasks
,
1987,
J. Algorithms.