#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 = 0for index in range(1,num+1):if alist[positionMax] < alist[index]:positionMax = index# temp = alist[num]# alist[num] = alist[positionMax]# alist[positionMax] =tempalist[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 = indexwhile alist[protion-1] > temp and protion > 0:alist[protion] = alist[protion-1]protion = protion-1alist[protion] = tempreturn 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 -= 1alist[left] = alist[right]while left < right and alist[left] <= temp:left += 1alist[right] = alist[left]alist[right] = tempreturn 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