Skip to content

Commit 482ff61

Browse files
authored
docs: Add type hints and improve documentation for pancake_sort
Updated docstring for pancake_sort function to include detailed explanations of parameters, return values, time complexity, and space complexity. Improved formatting and added examples for clarity.
1 parent a051ab5 commit 482ff61

File tree

1 file changed

+66
-23
lines changed

1 file changed

+66
-23
lines changed

sorts/pancake_sort.py

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,82 @@
1-
"""
2-
This is a pure Python implementation of the pancake sort algorithm
1+
"""Pancake Sort Algorithm Implementation.
2+
3+
This module provides a pure Python implementation of the Pancake Sort algorithm.
4+
Pancake sort is a sorting algorithm that sorts an array by repeatedly flipping
5+
subsections of the array, similar to how you might sort a stack of pancakes
6+
by inserting a spatula and flipping the top portion.
7+
8+
The algorithm works by finding the maximum element, flipping it to the top,
9+
then flipping it down to its correct position. This process is repeated for
10+
the remaining unsorted portion.
11+
12+
Time Complexity: O(n^2) - We perform n iterations, each with up to 2 flips
13+
Space Complexity: O(1) - In-place sorting, only uses a constant amount of extra space
14+
315
For doctests run following command:
4-
python3 -m doctest -v pancake_sort.py
16+
python3 -m doctest -v pancake_sort.py
517
or
6-
python -m doctest -v pancake_sort.py
18+
python -m doctest -v pancake_sort.py
719
For manual testing run:
8-
python pancake_sort.py
20+
python pancake_sort.py
921
"""
1022

23+
from typing import Any
24+
25+
26+
def pancake_sort(arr: list[Any]) -> list[Any]:
27+
"""Sort an array using the Pancake Sort algorithm.
28+
29+
Pancake sort works by finding the maximum unsorted element, flipping it to
30+
the top of the array, then flipping the entire unsorted portion to move the
31+
maximum to its correct position at the end.
32+
33+
Args:
34+
arr: A list of comparable items to be sorted.
35+
36+
Returns:
37+
The input list sorted in ascending order.
38+
39+
Time Complexity:
40+
O(n^2) where n is the length of the array.
41+
- We iterate n times (once for each position)
42+
- Each iteration involves finding max O(n) and up to 2 flips O(n)
43+
44+
Space Complexity:
45+
O(1) - sorting is done in-place with only constant extra space.
1146
12-
def pancake_sort(arr):
13-
"""Sort Array with Pancake Sort.
14-
:param arr: Collection containing comparable items
15-
:return: Collection ordered in ascending order of items
1647
Examples:
17-
>>> pancake_sort([0, 5, 3, 2, 2])
18-
[0, 2, 2, 3, 5]
19-
>>> pancake_sort([])
20-
[]
21-
>>> pancake_sort([-2, -5, -45])
22-
[-45, -5, -2]
48+
>>> pancake_sort([0, 5, 3, 2, 2])
49+
[0, 2, 2, 3, 5]
50+
>>> pancake_sort([])
51+
[]
52+
>>> pancake_sort([-2, -5, -45])
53+
[-45, -5, -2]
54+
>>> pancake_sort([1])
55+
[1]
56+
>>> pancake_sort([3, 1, 4, 1, 5, 9, 2, 6])
57+
[1, 1, 2, 3, 4, 5, 6, 9]
2358
"""
2459
cur = len(arr)
2560
while cur > 1:
26-
# Find the maximum number in arr
27-
mi = arr.index(max(arr[0:cur]))
28-
# Reverse from 0 to mi
29-
arr = arr[mi::-1] + arr[mi + 1 : len(arr)]
30-
# Reverse whole list
31-
arr = arr[cur - 1 :: -1] + arr[cur : len(arr)]
61+
# Find the index of maximum element in arr[0:cur]
62+
max_index = arr.index(max(arr[:cur]))
63+
64+
# Move maximum element to end of current unsorted portion:
65+
# 1. Flip to bring max to the beginning
66+
arr[: max_index + 1] = reversed(arr[: max_index + 1])
67+
# 2. Flip to send max to position cur-1
68+
arr[:cur] = reversed(arr[:cur])
69+
3270
cur -= 1
3371
return arr
3472

3573

3674
if __name__ == "__main__":
37-
user_input = input("Enter numbers separated by a comma:\n").strip()
75+
import doctest
76+
77+
doctest.testmod()
78+
79+
user_input = input("Enter numbers separated by comma: ").strip()
3880
unsorted = [int(item) for item in user_input.split(",")]
39-
print(pancake_sort(unsorted))
81+
print(f"Unsorted: {unsorted}")
82+
print(f"Sorted: {pancake_sort(unsorted)}")

0 commit comments

Comments
 (0)