Sorting by Merging or Merging by Sorting?

  • Gianni Franceschini
Part of the Lecture Notes in Computer Science book series (LNCS, volume 4059)


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 sn 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 Opt M (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(Opt M (X)) comparisons and O(Opt M (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))\).


Data Move Input Sequence Sorting Algorithm Merging Process Input Element 


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Franceschini, G., Geffert, V.: An In-Place Sorting with O(nlogn) Comparisons and O(n) Moves. Journal of the ACM 52, 515–537 (2005)CrossRefMathSciNetGoogle Scholar
  2. 2.
    Kronrod, M.A.: Optimal ordering algorithm without operational field. Soviet Math. Dokl. 10, 744–746 (1969)MATHGoogle Scholar
  3. 3.
    Levcopoulos, C., Overmars, M.H.: A balanced search tree with O(1) worst-case update time. Acta Informatica 26(3), 269–277 (1988)MATHCrossRefMathSciNetGoogle Scholar
  4. 4.
    Andersson, A., Lai, T.W.: Comparison–efficient and write–optimal searching and sorting. In: Hsu, W.-L., Lee, R.C.T. (eds.) ISA 1991. LNCS, vol. 557, pp. 273–282. Springer, Heidelberg (1991)Google Scholar
  5. 5.
    Fleischer, R.: A simple balanced search tree with O(1) worst-case update time. In: Ng, K.W., Balasubramanian, N.V., Raghavan, P., Chin, F.Y.L. (eds.) ISAAC 1993. LNCS, vol. 762, pp. 138–146. Springer, Heidelberg (1993)Google Scholar
  6. 6.
    Katajainen, J., Pasanen, T.: In-place sorting with fewer moves. Inform. Process. Lett. 70, 31–37 (1999)MATHCrossRefMathSciNetGoogle Scholar
  7. 7.
    Horvath, E.C.: Stable sorting in asymptotically optimal time and extra space. Journal of the ACM 25(2), 177–199 (1978)MATHCrossRefGoogle Scholar
  8. 8.
    Pardo, L.T.: Stable sorting and merging with optimal space and time bounds. SIAM Journal on Computing 6(2), 351–372 (1977)MATHCrossRefMathSciNetGoogle Scholar
  9. 9.
    Salowe, J., Steiger, W.: Simplified stable merging tasks. Journal of Algorithms 8(4), 557–571 (1987)MATHCrossRefMathSciNetGoogle Scholar
  10. 10.
    Huang, B.C., Langston, M.A.: Practical in-place merging. Communications of the ACM, CACM 31(3), 348–352 (1988)CrossRefGoogle Scholar
  11. 11.
    Huang, B.C., Langston, M.A.: Stable set and multiset operations in optimal time and space. In: ACM (ed.) PODS 1988. Proceedings of the Seventh ACM SIGACT-SIGMOD-SIGART Symposium on Principles of Database Systems, pp. 288–293. ACM Press, New York (1988)CrossRefGoogle Scholar
  12. 12.
    Symvonis, A.: Optimal stable merging. Comput. J. 38, 681–690 (1995)Google Scholar
  13. 13.
    Geffert, V., Katajainen, J., Pasanen, T.: Asymptotically efficient in-place merging. Theoret. Comput. Sci. 237, 159–181 (2000)MATHCrossRefMathSciNetGoogle Scholar
  14. 14.
    Chen, J.: Optimizing stable in-place merging. Theor. Comput. Sci. 1-3(302), 191–210 (2003)CrossRefGoogle Scholar
  15. 15.
    Estivill-Castro, V., Wood, D.: A survey of adaptive sorting algorithms. ACM Comp. Surveys 24(4), 441–476 (1992)CrossRefGoogle Scholar
  16. 16.
    Munro, J.: An implicit data structure supporting insertion, deletion, and search in O(log2 n) time. J. Comput. System Sci. 33, 66–74 (1986)MATHCrossRefMathSciNetGoogle Scholar
  17. 17.
    Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C.: Introduction to Algorithms. MIT Press, Cambridge (2001)MATHGoogle Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2006

Authors and Affiliations

  • Gianni Franceschini
    • 1
  1. 1.Institute for Informatics and TelematicsItalian National Research CouncilPisaItaly

Personalised recommendations