Python/sorts/pancake_sort.py
Mehdi ALAOUI 02c0daf9e5 Adding unit tests for sorting functions, and improving readability on some sorting algorithms (#784)
* Adding variable to fade out ambiguity

* More readability on merge sorting algorithm

* Updating merge_sort_fastest description and explaining why

* Adding tests file with imports

* Standardazing filenames and function names

* Adding test cases and test functions

* Adding test loop

* Putting 'user oriented code' inside main condition for having valid imports

* Fixing condition

* Updating tests: adding cases and todo list

* Refactoring first euler problem's first solution
2019-05-25 21:41:24 +08:00

18 lines
471 B
Python

# Pancake sort algorithm
# Only can reverse array from 0 to i
def pancake_sort(arr):
cur = len(arr)
while cur > 1:
# Find the maximum number in arr
mi = arr.index(max(arr[0:cur]))
# 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)]
cur -= 1
return arr
if __name__ == '__main__':
print(pancake_sort([0,10,15,3,2,9,14,13]))