Skip to content

Commit 829ba28

Browse files
committed
Pancake Sort
1 parent 6b94afa commit 829ba28

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Diff for: Sorting/烙饼排序(据说会考).py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sys
2+
3+
class Solution:
4+
# @param {int[]} A an integer array
5+
# @return nothing
6+
def sortIntegers(self, A):
7+
# Write your code here
8+
return self.pancakeSort(A, len(A))
9+
10+
def pancakeSort(self, array, length):
11+
for i in xrange(length - 1, 0, -1):
12+
cur_max_pos = self.findMax(array, i)
13+
print cur_max_pos
14+
if cur_max_pos != i:
15+
self.reverse(array, cur_max_pos)
16+
self.reverse(array, i)
17+
print 'here'
18+
print array
19+
return array
20+
21+
def findMax(self, array, pos):
22+
q = -sys.maxint
23+
result = None
24+
for i in xrange(pos + 1):
25+
if array[i] > q:
26+
q = array[i]
27+
result = i
28+
return result
29+
30+
def reverse(self, array, pos):
31+
st, ed = 0, pos
32+
while st < ed:
33+
array[st], array[ed] = array[ed], array[st]
34+
st += 1
35+
ed -= 1
36+
37+
a = Solution()
38+
print a.sortIntegers([5,3,4,2])
39+

0 commit comments

Comments
 (0)