# -*- coding: utf-8 -*-
def reverse(i):
global a
a = a[:i+1][::-1] + a[i+1:]
def issorted():
global a
for i in range(len(a)-1):
if a[i] > a[i+1]:
return False
return True
def search(step):
global max_step
global best_swap
global swap
if step > max_step:
return
if issorted():
if step <= max_step:
max_step = step
best_swap.append(swap[:step])
return
for i in range(1, len(a)):
if i != swap[step]:
reverse(i)
swap[step] = i
search(step + 1)
reverse(i)
a = [19, 21, 26, 43, 25]
max_step = 6
best_swap = []
swap = [0, 0, 0, 0, 0, 0, 0]
search(0)
print(best_swap)
【编程之美】一摞烙饼的排序python实现
于 2023-08-16 16:49:42 首次发布