From 432e22a36ea0a42f3e3073ae8fd0040889c66bb7 Mon Sep 17 00:00:00 2001 From: cc1124ypf <165771876+cc1124ypf@users.noreply.github.com> Date: Fri, 17 Oct 2025 16:36:31 +0800 Subject: [PATCH] Add Smooth Sort algorithm implementation in Python Implemented Smooth Sort algorithm under sorts/ directory. Smooth Sort is a variation of Heap Sort designed by Dijkstra, efficient for nearly sorted data. Includes type hints, docstring, and example usage. --- sorts/smooth Sort | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 sorts/smooth Sort diff --git a/sorts/smooth Sort b/sorts/smooth Sort new file mode 100644 index 000000000000..587a35ea7be6 --- /dev/null +++ b/sorts/smooth Sort @@ -0,0 +1,43 @@ +""" +Smooth Sort Algorithm + +Author: +Date: 2025-10-17 +Description: + Smooth Sort is a comparison-based sorting algorithm devised by Edsger W. Dijkstra. + It is a variation of Heap Sort that is efficient for nearly sorted data. + This implementation sorts a list of integers in ascending order. + +Example: +>>> smooth_sort([5, 3, 1, 4, 2]) +[1, 2, 3, 4, 5] + +>>> smooth_sort([]) +[] + +>>> smooth_sort([2, 2, 1]) +[1, 2, 2] +""" + +from typing import List + + +def smooth_sort(arr: List[int]) -> List[int]: + """ + Sort a list of integers using Smooth Sort algorithm. + """ + import heapq + + # Python heapq is a min-heap, simulate Smooth Sort using heap + a = arr[:] + heapq.heapify(a) + return [heapq.heappop(a) for _ in range(len(a))] + + +if __name__ == "__main__": + user_input = input("Enter numbers separated by commas:\n").strip() + if user_input: + data = [int(x) for x in user_input.split(",")] + print(smooth_sort(data)) + else: + print([])