|
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 | +
|
3 | 15 | For doctests run following command: |
4 | | -python3 -m doctest -v pancake_sort.py |
| 16 | + python3 -m doctest -v pancake_sort.py |
5 | 17 | or |
6 | | -python -m doctest -v pancake_sort.py |
| 18 | + python -m doctest -v pancake_sort.py |
7 | 19 | For manual testing run: |
8 | | -python pancake_sort.py |
| 20 | + python pancake_sort.py |
9 | 21 | """ |
10 | 22 |
|
| 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. |
11 | 46 |
|
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 |
16 | 47 | 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] |
23 | 58 | """ |
24 | 59 | cur = len(arr) |
25 | 60 | 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 | + |
32 | 70 | cur -= 1 |
33 | 71 | return arr |
34 | 72 |
|
35 | 73 |
|
36 | 74 | 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() |
38 | 80 | 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