Python编程之数据结构与算法练习_004
Some排序算法的Python实现。不废话写原理,直接撸代码。
1.Bubble sort 冒泡排序
import random import copy maxSize = 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 = False for j in range(0, maxSize - i - 1): if array[j] > array[j+1]: array[j], array[j+1] = array[j+1], array[j] swaped = True if 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 copy maxSize = 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 - 1 curIndex = left rightIndex = right while curIndex < rightIndex: if lst[curIndex] < partValue : leftIndex += 1 lst[curIndex], lst[leftIndex] = lst[leftIndex], lst[curIndex] curIndex += 1 elif lst[curIndex] > partValue: rightIndex -= 1 lst[curIndex], lst[rightIndex] = lst[rightIndex], lst[curIndex] else: curIndex += 1 else: 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 False else: return True maxRound = 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 random def merge(lst, left, middle, right): leftIndex = left rightIndex = middle+1 lstHelp = [] while leftIndex <= middle and rightIndex <= right: if lst[leftIndex] <= lst[rightIndex]: lstHelp.append(lst[leftIndex]) leftIndex += 1 else: lstHelp.append(lst[rightIndex]) rightIndex += 1 while leftIndex <= middle: lstHelp.append(lst[leftIndex]) leftIndex += 1 while rightIndex <= right: lstHelp.append(lst[rightIndex]) rightIndex += 1 for i in range(len(lstHelp)): lst[left+i] = lstHelp[i] def MergeSort(lst, left, right): if left == right: return middle = 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 False else: return True maxRound = 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.")