冒泡排序的的变种,时间复杂度跟冒泡排序一样
python版本:
def cocktail_sort(l):
bottom = 0;
top = l.__len__()
b_swap = True
while b_swap:
b_swap = False
for i in range(bottom, top-1):
if l[i] > l[i+1]:
l[i],l[i+1] = l[i+1],l[i]
b_swap = True
top-=1
for j in range(top -1, bottom, -1):
if l[j] < l[j-1]:
l[j],l[j-1] = l[j-1],l[j]
b_swap = True
bottom+=1