这个是一篇论文中鸡尾酒算法的改进版
————
鸡尾酒代码在这里:
def _cocktail_sort(the_list):
the_len = len(the_list)
if the_len <2:#0和1
print "无需排序"
return the_list
else:
while 1:
flag = False
for i in range(the_len-1):
if the_list[i] > the_list[i+1]:
the_list[i], the_list[i+1] = the_list[i+1], the_list[i]
print the_list
j = the_len-1
while j > 0:
if the_list[j-1] > the_list[j]:
the_list[j], the_list[j-1] = the_list[j-1], the_list[j]
flag = True
j -= 1
print the_list
if flag == False:
break
return the_list
if __name__ == '__main__':
the_list = [6, 4, 5, 1, 8, 7, 2, 3]
print "原始的列表是:" + str(the_list)
print "鸡尾酒排序之后的列表是:" + str(_cocktail_sort(the_list))
拜托拜托好心人 救救孩子吧!!!
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,目前超出我们的服务范围,暂时无法为您解答。
首次提问人员可免费体验一次有问必答服务。目前首次提问的问题服务范围为:编程语言、Java开发、python、数据库、前端开发 领域专业技术问题,为您提供问题的解决思路和指导。不提供源码代写、项目文档代写、论文代写、安装包资源发送或安装、软件使用指导等服务。
我们后续会持续优化,扩大我们的服务范围,为您带来更好地服务。