标签:实现 二分 shuffle imp not 递归 random col span
def f(n): if n == 1: return 1 elif n == 2: return 1 else: return f(n-1)+f(n-2) print(f(8))
def f(n): li = [0,1,1] if n <=2: return li[n] for i in range(3,n+1): li.append(li[-1]+li[-2]) return li[n] print(f(8))
O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(n2logn)<O(n3)
def hanoi(n,A,B,C): if n > 0: hanoi(n-1,A,C,B) print("%s->%s"%(A,C)) hanoi(n-1,B,A,C) hanoi(4,"A","B","C")
def binary_search(li, val): low = 0 high = len(li) - 1 while low <= high: mid = (low + high) // 2 if li[mid] < val: low = mid + 1 elif li[mid] > val: high = mid -1 else: return mid return None
import random def bubble_sort(li): for i in range(len(li)-1): # i表示第i趟,共n-1趟 # 第i趟 无序区范围 0~n-i-1 for j in range(len(li)-i-1): if li[j] > li[j+1]: li[j],li[j+1] = li[j+1],li[j] print(li) li = [8,5,7,9,4,2,6,1,3] bubble_sort(li)
@cal_time def bubble_sort_2(li): for i in range(len(li)-1): exchange = False for j in range(len(li)-1): if li[j] > li[j+1]: li[j],li[j+1] = li[j+1],li[j] exchange = True if not exchange: return li=list(range(10000)) random.shuffle(li) bubble_sort_2(li)
标签:实现 二分 shuffle imp not 递归 random col span
原文地址:https://www.cnblogs.com/hnlmy/p/9845965.html