1. 选择排序
--
--
def selectSort(l): for i in range(len(l)): j = i + 1 t_min = l[i] loc_min = i for j in range(j,len(l)): if l[j] < t_min: t_min = l[j] loc_min = j if loc_min != i: t = l[i] l[i] = t_min l[loc_min] = t
2. 冒泡排序
--
--
def bubbleSort(l): for i in range(len(l)-1,1,-1): for j in range(i): if l[j] > l[j+1]: t = l[j+1] l[j+1] = l[j] l[j] = t
3. 插入排序
--
--
def insertSort(param): p_len = len(param) for i in range(1,p_len): key = param[i] #for j in range(1,i+1)[::-1]: for j in range(i,0,-1): #if j>0 and key < param[j-1]: if key < param[j-1]: param[j] = param[j-1] param[j-1] = key
4. 桶排序
--
--
def bucketSort(l): min_,max_ = min_max(l) bucket_len = max_ - min_ + 1 bucketList = [0] * bucket_len rList = [] for e in l: bucketList[e-min_] += 1 for i in range(bucket_len): if bucketList[i] != 0: for j in range(bucketList[i]): rList.append(i + min_) return rList def min_max(l): min_ = max_ = l[0] for e in l: if min_ > e: min_ = e if max_ < e: max_ = e return min_,max_