2019-07-02 04:05:43 +00:00
|
|
|
"""Pancake Sort Algorithm."""
|
2018-03-04 02:27:05 +00:00
|
|
|
# Only can reverse array from 0 to i
|
|
|
|
|
2019-07-02 04:05:43 +00:00
|
|
|
|
2019-05-25 13:41:24 +00:00
|
|
|
def pancake_sort(arr):
|
2019-07-02 04:05:43 +00:00
|
|
|
"""Sort Array with Pancake Sort."""
|
2018-03-04 02:27:05 +00:00
|
|
|
cur = len(arr)
|
|
|
|
while cur > 1:
|
|
|
|
# Find the maximum number in arr
|
|
|
|
mi = arr.index(max(arr[0:cur]))
|
2019-07-02 04:05:43 +00:00
|
|
|
# Reverse from 0 to mi
|
|
|
|
arr = arr[mi::-1] + arr[mi + 1:len(arr)]
|
|
|
|
# Reverse whole list
|
|
|
|
arr = arr[cur - 1::-1] + arr[cur:len(arr)]
|
2018-03-04 02:27:05 +00:00
|
|
|
cur -= 1
|
|
|
|
return arr
|
|
|
|
|
2019-07-02 04:05:43 +00:00
|
|
|
|
2019-05-25 13:41:24 +00:00
|
|
|
if __name__ == '__main__':
|
2019-07-02 04:05:43 +00:00
|
|
|
print(pancake_sort([0, 10, 15, 3, 2, 9, 14, 13]))
|