Created pancake_sort.py in algorithms/sorts by 9967han · Pull Request #336 · keon/algorithms · GitHub
Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ pip3 uninstall -y algorithms
- [insertion_sort:插入排序](algorithms/sort/insertion_sort.py)
- [meeting_rooms:会议室](algorithms/sort/meeting_rooms.py)
- [merge_sort:归并排序](algorithms/sort/merge_sort.py)
- [pancake_sort](algorithms/sort/pancake_sort.py)
- [quick_sort:快速排序](algorithms/sort/quick_sort.py)
- [radix_sort](algorithms/sort/radix_sort.py)
- [selection_sort:选择排序](algorithms/sort/selection_sort.py)
Expand Down
1 change: 1 addition & 0 deletions README_GE.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
- [insertion_sort](algorithms/sort/insertion_sort.py)
- [meeting_rooms](algorithms/sort/meeting_rooms.py)
- [merge_sort](algorithms/sort/merge_sort.py)
- [pancake_sort](algorithms/sort/pancake_sort.py)
- [quick_sort](algorithms/sort/quick_sort.py)
- [radix_sort](algorithms/sort/radix_sort.py)
- [selection_sort](algorithms/sort/selection_sort.py)
Expand Down
1 change: 1 addition & 0 deletions README_JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ if __name__ == "__main__":
- [insertion_sort](algorithms/sort/insertion_sort.py)
- [meeting_rooms](algorithms/sort/meeting_rooms.py)
- [merge_sort](algorithms/sort/merge_sort.py)
- [pancake_sort](algorithms/sort/pancake_sort.py)
- [quick_sort](algorithms/sort/quick_sort.py)
- [radix_sort](algorithms/sort/radix_sort.py)
- [selection_sort](algorithms/sort/selection_sort.py)
Expand Down
1 change: 1 addition & 0 deletions README_KR.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ if __name__ == "__main__":
- [insertion_sort](algorithms/sort/insertion_sort.py)
- [meeting_rooms](algorithms/sort/meeting_rooms.py)
- [merge_sort](algorithms/sort/merge_sort.py)
- [pancake_sort](algorithms/sort/pancake_sort.py)
- [quick_sort](algorithms/sort/quick_sort.py)
- [radix_sort](algorithms/sort/radix_sort.py)
- [selection_sort](algorithms/sort/selection_sort.py)
Expand Down
1 change: 1 addition & 0 deletions algorithms/sort/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .heap_sort import *
from .insertion_sort import *
from .merge_sort import *
from .pancake_sort import *
from .quick_sort import *
from .selection_sort import *
from .top_sort import *
Expand Down
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
5 changes: 5 additions & 0 deletions tests/test_sort.py