Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions algorithms/sort/pancake_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def pancake_sort(arr):
"""
Pancake_sort
Sorting a given array
mutation of selection sort

reference: https://www.geeksforgeeks.org/pancake-sorting/

Overall time complexity : O(N^2)
"""

len_arr = len(arr)
if len_arr <= 1:
return arr
for cur in range(len(arr), 1, -1):
#Finding index of maximum number in arr
index_max = arr.index(max(arr[0:cur]))
if index_max+1 != cur:
#Needs moving
if index_max != 0:
#reverse from 0 to index_max
arr[:index_max+1] = reversed(arr[:index_max+1])
# Reverse list
arr[:cur] = reversed(arr[:cur])
return arr