#coding=utf-8'''数据结构排序'''#函数冒泡排序# 参数alist:被排序的列表def bubbleSort(alist): for num in range(len(alist)-1,0,-1): for i in range(num): if alist[i] < alist[i+1]: #进行当前位置和下一个位置的交换 alist[i] = alist[i]^alist[i+1] alist[i+1] = alist[i]^alist[i+1] alist[i] = alist[i]^alist[i+1] return alist#函数选择排序# 参数alist:被排序的列表def selectSort(alist): for num in range(len(alist)-1,0,-1): positionMax = 0 for index in range(1,num+1): if alist[positionMax] < alist[index]: positionMax = index # temp = alist[num] # alist[num] = alist[positionMax] # alist[positionMax] =temp alist[num] = alist[num]^alist[positionMax] alist[positionMax] = alist[num]^alist[positionMax] alist[num] = alist[num]^alist[positionMax] print alist[num] return alist#函数插入排序# 参数alist:被排序的列表def insertSort(alist): for index in range(1,len(alist)): temp = alist[index] protion = index while alist[protion-1] > temp and protion > 0: alist[protion] = alist[protion-1] protion = protion-1 alist[protion] = temp return alistnumber=[54,26,93,17,77,31,44,55,21]#快速排序# 参数alist:被排序的列表# 参数low:左侧起始位置# 参数hegh:右侧终止位置def quickSort(alist,low,hegh): if low < hegh: pos = findpos(alist,low,hegh) quickSort(alist, low, pos-1) quickSort(alist, pos+1, hegh) return alist#快排中查找中间节点# 参数alist:被产讯的列表# 参数low:左侧起始位置# 参数hegh:右侧终止位置def findpos(alist,left,right): temp = alist[left] while left < right: while left < right and alist[right] >= temp: right -= 1 alist[left] = alist[right] while left < right and alist[left] <= temp: left += 1 alist[right] = alist[left] alist[right] = temp return rightif __name__ == "__main__": # #测试冒泡排序: # num = bubbleSort(number) # print num # #调用选择排序 # num = selectSort(number) # print num #调入插入排序 num = insertSort(number) print num # #快速排序 # num = quickSort(number, 0, len(number)-1) # print num