Skip to content

Commit 432e22a

Browse files
authored
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.
1 parent c79034c commit 432e22a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

sorts/smooth Sort

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Smooth Sort Algorithm
3+
4+
Author: <cc1124ypf>
5+
Date: 2025-10-17
6+
Description:
7+
Smooth Sort is a comparison-based sorting algorithm devised by Edsger W. Dijkstra.
8+
It is a variation of Heap Sort that is efficient for nearly sorted data.
9+
This implementation sorts a list of integers in ascending order.
10+
11+
Example:
12+
>>> smooth_sort([5, 3, 1, 4, 2])
13+
[1, 2, 3, 4, 5]
14+
15+
>>> smooth_sort([])
16+
[]
17+
18+
>>> smooth_sort([2, 2, 1])
19+
[1, 2, 2]
20+
"""
21+
22+
from typing import List
23+
24+
25+
def smooth_sort(arr: List[int]) -> List[int]:
26+
"""
27+
Sort a list of integers using Smooth Sort algorithm.
28+
"""
29+
import heapq
30+
31+
# Python heapq is a min-heap, simulate Smooth Sort using heap
32+
a = arr[:]
33+
heapq.heapify(a)
34+
return [heapq.heappop(a) for _ in range(len(a))]
35+
36+
37+
if __name__ == "__main__":
38+
user_input = input("Enter numbers separated by commas:\n").strip()
39+
if user_input:
40+
data = [int(x) for x in user_input.split(",")]
41+
print(smooth_sort(data))
42+
else:
43+
print([])

0 commit comments

Comments
 (0)