Skip to content

Commit ec98ef9

Browse files
2400031832cclauss
andauthored
docs: Add type hints and improve documentation for pancake_sort (#13960)
* 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. * Apply batched suggestions from code review Co-authored-by: Christian Clauss <cclauss@me.com> --------- Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent b5aa7be commit ec98ef9

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

‎sorts/pancake_sort.py‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
"""
2-
This is a pure Python implementation of the pancake sort algorithm
1+
"""Pancake Sort Algorithm Implementation.
2+
3+
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
314
415
For doctests run following command:
5-
python3 -m doctest -v pancake_sort.py
16+
python3 -m doctest -v pancake_sort.py
617
or
7-
python -m doctest -v pancake_sort.py
18+
python -m doctest -v pancake_sort.py
819
For manual testing run:
9-
python pancake_sort.py
20+
python pancake_sort.py
1021
"""
1122

1223
from collections.abc import Sequence
@@ -51,12 +62,13 @@ def pancake_sort[T: Comparable](arr: Sequence[T]) -> list[T]:
5162
arr = list(arr)
5263
cur = len(arr)
5364
while cur > 1:
54-
# Find the maximum number in arr
55-
mi = arr.index(max(arr[0:cur]))
56-
# Reverse from 0 to mi
57-
arr = arr[mi::-1] + arr[mi + 1 : len(arr)]
58-
# Reverse whole list
59-
arr = arr[cur - 1 :: -1] + arr[cur : len(arr)]
65+
# Find the index of maximum element in arr[0:cur]
66+
max_index = arr.index(max(arr[:cur]))
67+
# Move maximum element to end of current unsorted portion:
68+
# 1. Flip to bring max to the beginning
69+
arr[: max_index + 1] = reversed(arr[: max_index + 1])
70+
# 2. Flip to send max to position cur-1
71+
arr[:cur] = reversed(arr[:cur])
6072
cur -= 1
6173
return arr
6274

@@ -67,4 +79,4 @@ def pancake_sort[T: Comparable](arr: Sequence[T]) -> list[T]:
6779
doctest.testmod()
6880
user_input = input("Enter numbers separated by a comma:\n").strip()
6981
unsorted = [int(item) for item in user_input.split(",")]
70-
print(f"{pancake_sort(unsorted) = }")
82+
print(f"{unsorted = }, {pancake_sort(unsorted) = }")

0 commit comments

Comments
 (0)