Python编程之数据结构与算法练习_004
生活随笔
收集整理的這篇文章主要介紹了
Python编程之数据结构与算法练习_004
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Some排序算法的Python實(shí)現(xiàn)。不廢話寫原理,直接擼代碼。
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] #將用來(lái)劃分的值交換至最終位置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.")
?
轉(zhuǎn)載于:https://www.cnblogs.com/orcsir/p/8762906.html
總結(jié)
以上是生活随笔為你收集整理的Python编程之数据结构与算法练习_004的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 人教版五年级计算机教案,人教版信息技术五
- 下一篇: Python实现比较两个列表(list)