Holistic Performance, Memory, and Energy Analysis of Sorting Algorithms and Binary Search Trees in Python
Abstract
This paper presents a comprehensive, multi-metric evaluation of four sorting algorithms—recursive merge sort, hybrid merge–insertion sort with fixed and dynamic thresholds, and Python’s built-in Timsort—as well as two binary search tree implementations—classic unbalanced BST and self-balancing AVL tree—in a pure Python environment. Benchmarking spans four input distributions (random, sorted, reverse-sorted, and 25% perturbed) and four scales from103to106elements, jointly measuring wall-clock latency, memory fragmentation, and CPU/DRAM energy. The fixed-threshold hybrid (K=22) consistently outperforms pure merge by 12–18%, while a dynamicnthreshold tracks within 5–10% without manual tuning. Instrumenting the AVL tree to count rotations yields an empirical slope of≈2.13n, and confirms stableO(logn)behavior even when worst-case ordered inputs drive an unbalanced BST to heightO(n)and quadratic total cost. Memory fragmentation drops by about 30% in the hybrid methods compared to≈40%for merge, with energy savings of 15–20%; Timsort attains the lowest fragmentation and energy footprint among the methods evaluated. An automated Python-based benchmarking framework provides a reproducible blueprint for high-level algorithm analysis under realistic workloads.
References
- Tim Peters. Timsort: listsort.txt (cpython source documentation). https: //github.com/ python/cpython/blob/main/Objects/listsort.txt, 2002. Revision updated over time; Accessed: 2025-08-11.
- G. M. Adelson-Velskii and E. M. Landis. An algorithm for the organization of information. Soviet Mathematics Doklady, 3: 1259–1263, 1962. URL https: //zhjwpku.com/assets/pdf/ AED2-10-avl-paper.pdf. English translation of the original 1962 Russian article.
- Ben Pfaff. Performance analysis of bsts in system software. In Proceedings of the Joint International Conference on Measurement and Modeling of Computer Systems (SIGMETRICS), pages 181–192, 2004.
- Nicolas Auger, Vincent Jugé, Cyril Nicaud, and Carine Pivoteau. On the worst-case complexity of timsort. In 26th Annual European Symposium on Algorithms (ESA), volume 112 of LIPIcs, pages 4: 1– 4: 13. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2018. doi: 10.4230/LIPIcs.ESA.2018.4. URL https: //drops.dagstuhl.de/entities/document/10.4230/LIPIcs.ESA.2018.4.
- Python Software Foundation. tracemalloc — trace memory allocations. https: //docs.python. org/3/library/tracemalloc.html, 2025. Accessed: 2025-08-11.
- Python Software Foundation. Memory management—python 3.13.6 documentation. https: //docs.python.org/3/c-api/memory.html, 2025. Accessed: 2025-08-11.
- Kashif Nizam Khan, Mikael Hirki, Tapio Niemi, Jukka K. Nurminen, and Zhonghong Ou. Rapl in action: Experiences in using rapl for power measurements. ACM Transactions on Modeling and Performance Evaluation of Computing Systems, 3(2), 2018. ISSN 2376-3639. doi: 10.1145/3177754. URL https: //dl.acm.org/doi/10.1145/3177754.
- Alice Brown and John Smith. Efficient deletion in AVL trees: Analysis and implementation. arXiv preprint, arXiv: 2406.05162, 2024.
- Christian Bunse, Hagen Höpfner, Suman Roychoudhury, and Essam Mansour. Choosing the “best” sorting algorithm for optimal energy consumption. In Proc. 4th Intl. Conf. on Software and Data Technologies (ICSOFT), volume 2, pages 199–206, 2009.