冒泡排序对数器测试
def bubbleSort(L):"""冒泡排序,每一次1、2比較,2、3比較....每一次最大的是最后一個時間復雜度:O(N^2)"""if len(L)==0 or len(L)==1:return Lfor end in range(len(L)-1,0,-1):for i in range(0,end):if L[i] > L[i+1]:swap(L,i,j)return Ldef swap(L,i,j):L[i] = L[i]^L[j]L[j] = L[i]^L[j]L[i] = L[i]^L[j]L = [1,3,5,4,2,6,8,9,10,0]
sorted(L)
bubbleSort(L)//對數器進行測試def generateRandomList(maxSize,maxValue):import randomLL = [random.random()] * (maxSize + 1)for i in range(len(LL)):LL[i] = int((maxValue + 1) * random.random()) - int(maxValue * random.random())return LLdef isEqual(L1,L2):if (len(L1)==0 and len(L2)!=0) or (len(L1)!=0 and len(L2)==0):return Falseif len(L1)==0 and len(L2)==0:return Trueif len(L1)!=len(L2):return Falsefor i in range(len(L1)):if L1[i]!=L2[i]:return Falsereturn Truedef main():testTime = 10000maxSize = 100maxValue = 100succeed = Truefor i in range(testTime):L1 = generateRandomList(maxSize,maxValue)bubbleSort(L1)L3 = sorted(L1)if isEqual(L1,L3) is False: succeed = Falsebreakif succeed:print("Nice")else:print("Fucking....")print(L1)print("\n")print(L3)main()
?
總結
- 上一篇: 对数器
- 下一篇: 递归行为时间复杂度估算