# Programming Techniques for Reversible Comparison Sorts

## Abstract

A common approach to reversible programming is to reversibly simulate an irreversible program with the desired functionality, which in general puts additional pressure on the computational resources (time, space.) If the same running time is required, ensuring a minimal space overhead is a significant programming challenge.

We introduce criteria for the optimality of reversible simulation: A reversible simulation is *faithful* if it incurs no asymptotic time overhead and bounds the space overhead (the *garbage*) by some function *g*(*n*), and *hygienic* if *g* is (asymptotically) optimal for faithful simulation.

We demonstrate the programming techniques used to develop faithful and hygienic reversible simulations of several well-known comparison sorts, *e.g.* insertion sort and quicksort, using representations of permutations in both the output and intermediate additional space required.

## Keywords

Procedure Call Space Usage Factorial Representation Identity Permutation Sorting Network## Notes

### Acknowledgements

H.B. Axelsen was supported by the Danish Council for Independent Research \(\mid \) Natural Sciences under the *Foundations of Reversible Computing* project. T. Yokoyama was supported by MEXT KAKENHI 25730049.

## Supplementary material

## References

- 1.Axelsen, H.B., Glück, R.: What do reversible programs compute? In: Hofmann, M. (ed.) FOSSACS 2011. LNCS, vol. 6604, pp. 42–56. Springer, Heidelberg (2011) CrossRefGoogle Scholar
- 2.Axelsen, H.B., Thomsen, M.K.: Garbage-free reversible integer multiplication with constants of the form 2\(^{k}\pm \)2\(^l\pm \)1. In: Glück, R., Yokoyama, T. (eds.) RC 2012. LNCS, vol. 7581, pp. 171–182. Springer, Heidelberg (2013)CrossRefGoogle Scholar
- 3.Bennett, C.H.: Time/space trade-offs for reversible computation. SIAM J. Comput.
**18**(4), 766–776 (1989)CrossRefMathSciNetzbMATHGoogle Scholar - 4.Bonet, B.: Efficient algorithms to rank and unrank permutations in lexicographic order. In: Workshop on Search in Artificial Intelligence and Robotics. AAAI (2008)Google Scholar
- 5.Dijkstra, E.W.: Program inversion. In: Bauer, F.L., Broy, M. (eds.) Program Construction: International Summer School. LNCS, vol. 69, pp. 54–57. Springer, Heidelberg (1979)CrossRefGoogle Scholar
- 6.Early, D., Gao, A., Schellekens, M.: Frugal encoding in reversible \({\cal MOQA}\): a case study for quicksort. In: Glück, R., Yokoyama, T. (eds.) RC 2012. LNCS, vol. 7581, pp. 85–96. Springer, Heidelberg (2013)CrossRefGoogle Scholar
- 7.Hall, J.S.: A reversible instruction set architecture and algorithms. In: Proceedings of Physics and Computation, pp. 128–134. IEEE Press, New York (1994)Google Scholar
- 8.Knuth, D.E.: The Art of Computer Programming, Volume 3: Sorting and Searching, 2nd edn. Addison Wesley Longman Publishing Co. Inc., Boston (1998)Google Scholar
- 9.Lutz, C.: Janus: A time-reversible language. Letter to R. Landauer (1986)Google Scholar
- 10.Myrvold, W., Ruskey, F.: Ranking and unranking permutations in linear time. Inf. Proc. Let.
**79**(6), 281–284 (2001)CrossRefMathSciNetzbMATHGoogle Scholar - 11.Nishida, N., Vidal, G.: Program inversion for tail recursive functions. In: Schmidt-Schauß, M. (ed.) RTA. LIPIcs, vol. 10, pp. 283–298. Schloss Dagstuhl–Leibniz-Zentrum für Informatik, Dagstuhl (2011) Google Scholar
- 12.Perumalla, K.S.: Introduction to Reversible Computing. CRC Press, Boca Raton (2013)Google Scholar
- 13.Wille, R., Drechsler, R.: Towards a Design Flow for Reversible Logic. Springer, Heidelberg (2010)CrossRefzbMATHGoogle Scholar
- 14.Yokoyama, T., Axelsen, H.B., Glück, R.: Principles of a reversible programming language. In: Proceedings of Computing Frontiers, pp. 43–54. ACM Press, New York (2008)Google Scholar
- 15.Yokoyama, T., Axelsen, H.B., Glück, R.: Minimizing garbage size by generating reversible simulations. In: Proceedings of Networking and Computing, pp. 379–387. IEEE Press, New York (2012)Google Scholar