冒泡算法:
#-*- coding: UTF-8 -*-
#冒泡排序
def func(lt):
if type(lt).__name__ !='list' and type(lt).__name__ !='tuple':
return
if type(lt).__name__ == 'tuple':
return list(lt)
for i in range(1,len(lt)-1):
for j in range(1,len(lt)-i):
if lt[j] > lt[j+1]:
lt[j],lt[j+1] = lt[j+1],lt[j]
return lt
lt_01 = [2,34,5,14,7,28]
print func(lt_01)
=================================================
二分法:(通过函数递归调用实现)
def func(lt=[]):
if type(lt).__name__ !='list' and type(lt).__name__!='tuple':
return
if type(lt).__name__ =='tuple':
return list(lt)
if len(lt)<=1:
return lt
k = lt[0]
lt_l = []
lt_r = []
lt_m = []
for i in lt:
if i>k:
lt_r.append(i)
elif i<k:
lt_l.append(i)
else:
lt_m.append(i)
lt_l=func(lt_l)
lt_r=func(lt_r)
return lt_l + lt_m + lt_r
a = [2,4,12,6,23,16,28]
print func(a)