MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / pancake_sort

Function pancake_sort

sorts/pancake_sort.py:12–33  ·  view source on GitHub ↗

Sort Array with Pancake Sort. :param arr: Collection containing comparable items :return: Collection ordered in ascending order of items Examples: >>> pancake_sort([0, 5, 3, 2, 2]) [0, 2, 2, 3, 5] >>> pancake_sort([]) [] >>> pancake_sort([-2, -5, -45]) [-45, -5, -

(arr)

Source from the content-addressed store, hash-verified

10
11
12def 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 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]
23 """
24 cur = len(arr)
25 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)]
32 cur -= 1
33 return arr
34
35
36if __name__ == "__main__":

Callers 1

pancake_sort.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected