Skip to content

Commit f9f8746

Browse files
9967hanHai Hoang Dang
authored andcommitted
Created pancake_sort.py in algorithms/sorts (keon#336)
* Update README.md * Update README_CN.md * Update README_GE.md * Update README_JP.md * Update README_KR.md * Update test_sort.py * Create pancake_sort.py * Update __init__.py
1 parent ffe1c21 commit f9f8746

File tree

8 files changed

+36
-0
lines changed

8 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ If you want to uninstall algorithms, it is as simple as:
235235
- [insertion_sort](algorithms/sort/insertion_sort.py)
236236
- [meeting_rooms](algorithms/sort/meeting_rooms.py)
237237
- [merge_sort](algorithms/sort/merge_sort.py)
238+
- [pancake_sort](algorithms/sort/pancake_sort.py)
238239
- [quick_sort](algorithms/sort/quick_sort.py)
239240
- [radix_sort](algorithms/sort/radix_sort.py)
240241
- [selection_sort](algorithms/sort/selection_sort.py)

README_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ pip3 uninstall -y algorithms
221221
- [insertion_sort:插入排序](algorithms/sort/insertion_sort.py)
222222
- [meeting_rooms:会议室](algorithms/sort/meeting_rooms.py)
223223
- [merge_sort:归并排序](algorithms/sort/merge_sort.py)
224+
- [pancake_sort](algorithms/sort/pancake_sort.py)
224225
- [quick_sort:快速排序](algorithms/sort/quick_sort.py)
225226
- [radix_sort](algorithms/sort/radix_sort.py)
226227
- [selection_sort:选择排序](algorithms/sort/selection_sort.py)

README_GE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
242242
- [insertion_sort](algorithms/sort/insertion_sort.py)
243243
- [meeting_rooms](algorithms/sort/meeting_rooms.py)
244244
- [merge_sort](algorithms/sort/merge_sort.py)
245+
- [pancake_sort](algorithms/sort/pancake_sort.py)
245246
- [quick_sort](algorithms/sort/quick_sort.py)
246247
- [radix_sort](algorithms/sort/radix_sort.py)
247248
- [selection_sort](algorithms/sort/selection_sort.py)

README_JP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ if __name__ == "__main__":
236236
- [insertion_sort](algorithms/sort/insertion_sort.py)
237237
- [meeting_rooms](algorithms/sort/meeting_rooms.py)
238238
- [merge_sort](algorithms/sort/merge_sort.py)
239+
- [pancake_sort](algorithms/sort/pancake_sort.py)
239240
- [quick_sort](algorithms/sort/quick_sort.py)
240241
- [radix_sort](algorithms/sort/radix_sort.py)
241242
- [selection_sort](algorithms/sort/selection_sort.py)

README_KR.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ if __name__ == "__main__":
232232
- [insertion_sort](algorithms/sort/insertion_sort.py)
233233
- [meeting_rooms](algorithms/sort/meeting_rooms.py)
234234
- [merge_sort](algorithms/sort/merge_sort.py)
235+
- [pancake_sort](algorithms/sort/pancake_sort.py)
235236
- [quick_sort](algorithms/sort/quick_sort.py)
236237
- [radix_sort](algorithms/sort/radix_sort.py)
237238
- [selection_sort](algorithms/sort/selection_sort.py)

algorithms/sort/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .heap_sort import *
77
from .insertion_sort import *
88
from .merge_sort import *
9+
from .pancake_sort import *
910
from .quick_sort import *
1011
from .selection_sort import *
1112
from .top_sort import *

algorithms/sort/pancake_sort.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def pancake_sort(arr):
2+
"""
3+
Pancake_sort
4+
Sorting a given array
5+
mutation of selection sort
6+
7+
reference: https://www.geeksforgeeks.org/pancake-sorting/
8+
9+
Overall time complexity : O(N^2)
10+
"""
11+
12+
len_arr = len(arr)
13+
if len_arr <= 1:
14+
return arr
15+
for cur in range(len(arr), 1, -1):
16+
#Finding index of maximum number in arr
17+
index_max = arr.index(max(arr[0:cur]))
18+
if index_max+1 != cur:
19+
#Needs moving
20+
if index_max != 0:
21+
#reverse from 0 to index_max
22+
arr[:index_max+1] = reversed(arr[:index_max+1])
23+
# Reverse list
24+
arr[:cur] = reversed(arr[:cur])
25+
return arr

tests/test_sort.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
max_heap_sort, min_heap_sort,
88
insertion_sort,
99
merge_sort,
10+
pancake_sort,
1011
quick_sort,
1112
selection_sort,
1213
bucket_sort,
@@ -60,6 +61,10 @@ def test_merge_sort(self):
6061
self.assertEqual([1, 5, 23, 57, 65, 1232],
6162
merge_sort([1, 5, 65, 23, 57, 1232]))
6263

64+
def test_pancake_sort(self):
65+
self.assertEqual([1, 5, 23, 57, 65, 1232],
66+
pancake_sort([1, 5, 65, 23, 57, 1232]))
67+
6368
def test_quick_sort(self):
6469
self.assertEqual([1, 5, 23, 57, 65, 1232],
6570
quick_sort([1, 5, 65, 23, 57, 1232]))

0 commit comments

Comments
 (0)