Some排序算法的Python实现。不废话写原理,直接撸代码。
1.Bubble sort 冒泡排序
import random import copymaxSize = 10000 maxValue = 10000#Generate random data array=[0]*maxSize for i in range(maxSize):array[i] = random.randint(10,maxValue)#Correct method originalArray = copy.deepcopy(array) testArray = copy.deepcopy(array) testArray.sort()#Bubble sort swaped = False for i in range(maxSize):swaped = Falsefor j in range(0, maxSize - i - 1):if array[j] > array[j+1]:array[j], array[j+1] = array[j+1], array[j]swaped = Trueif not swaped:break #Compare for i in range(maxSize):if testArray[i] != array[i]:print("Fucked.")print(originalArray)break else:print("Success.")
2.Insertion sort 插入排序
#插入排序 import random import copymaxSize = 10000 maxValue = 10000#Generate random data array=[0]*maxSize for i in range(maxSize):array[i] = random.randint(10,maxValue) #Correct method testArray = copy.deepcopy(array) testArray.sort()#Insertion sort for i in range(1, maxSize):for j in range(i, 0, -1):if array[j] <= array[j-1]:array[j],array[j-1] = array[j-1],array[j]else:break #Compare for i in range(maxSize):if testArray[i] != array[i]:print("Fucked.")print("Fucked sample:{}".format(lst))break else:print("Success.")
3.Quick sort 快速排序
def Partition(lst, left,right):partValue = lst[right]leftIndex = left - 1curIndex = leftrightIndex = rightwhile curIndex < rightIndex:if lst[curIndex] < partValue :leftIndex += 1 lst[curIndex], lst[leftIndex] = lst[leftIndex], lst[curIndex]curIndex += 1elif lst[curIndex] > partValue:rightIndex -= 1lst[curIndex], lst[rightIndex] = lst[rightIndex], lst[curIndex]else:curIndex += 1else:lst[rightIndex], lst[right] = lst[right], lst[rightIndex] #将用来划分的值交换至最终位置return [leftIndex ,rightIndex+1 ]def QuickSort(lst, left, right):if left < right :lstTmp = Partition(lst,left,right)QuickSort(lst, left, lstTmp[0])QuickSort(lst, lstTmp[1], right)# Test def Compare(lstX, lstY):for i in range(len(lstX)):if lstX[i] != lstY[i]:return Falseelse:return TruemaxRound = 10000 maxValue = 1000 arrLength = 10000 for i in range(maxRound):lst = [random.randint(-maxValue,maxValue) for i in range(arrLength)]
lstCopy = copy.deepcopy(lst)QuickSort(lst, 0, len(lst) - 1)correctResult = sorted(lstCopy)if not Compare(correctResult, lst):print("Fucked.")print("Fucked sample:{}".format(lst))break else:print("Success.")
4.Merge sort 归并排序
#归并排序import randomdef merge(lst, left, middle, right):leftIndex = leftrightIndex = middle+1lstHelp = []while leftIndex <= middle and rightIndex <= right:if lst[leftIndex] <= lst[rightIndex]:lstHelp.append(lst[leftIndex])leftIndex += 1else:lstHelp.append(lst[rightIndex])rightIndex += 1while leftIndex <= middle:lstHelp.append(lst[leftIndex])leftIndex += 1while rightIndex <= right:lstHelp.append(lst[rightIndex])rightIndex += 1for i in range(len(lstHelp)):lst[left+i] = lstHelp[i]def MergeSort(lst, left, right):if left == right:returnmiddle = left + ((right - left)>>1)MergeSort(lst, left, middle,)MergeSort(lst, middle+1, right)merge(lst, left, middle, right)# Test def Compare(lstX, lstY):for i in range(len(lstX)):if lstX[i] != lstY[i]:return Falseelse:return TruemaxRound = 10000 maxValue = 1000 arrLength = 1000 for i in range(maxRound):lst = [random.randint(-maxValue,maxValue) for _ in range(arrLength)]
lstCopy = copy.deepcopy(lst)MergeSort(lst, 0, len(lst) - 1)correctResult = sorted(lstCopy)if not Compare(correctResult, lst):print("Fucked.")print("Fucked sample:{}".format(lst))break else:print("Success.")