# import time # start_time =time.time() # for a in range(1001): # for b in range(1001): # for c in range(1001): # if a + b + c ==1000 and a**2 + b**2 ==c**2: # print(a,b,c) # end_time = time.time() # print(end_time - start_time ) ''' 1.算法的5大特性:出入有定型 输入性:有0个或多个外部量作为算法的输入 输出性:算法至少有一个量输出 确定性:每条指令清晰 有穷性:每条执行次数有限 可行性:能够精准运行 程序的 ''' # import time # def sumofn(n): # # start_time =time.time() # sum = 0 # for i in range(1 , n+1): # sum += i # # end_time = time.time() -start_time # return sum,end_time # for i in range(5): # result,time1 = sumofn(1000000000) # print(result,time1) # import time # def a(n): # start_time =time.time() # for a in range(n): # for b in range(n): # for c in range(n): # if a + b + c ==1000 and a**2 + b**2 ==c**2: # print(a,b,c) # end_time = time.time() # print(end_time - start_time ) # return a,b,c # print(a(1000)) # import time # def n2(n): # s = time.time() # r = (n * (n - 1) / 2)**2 # for i in range(1,100): # for a in range (1, 100): # for b in range(1, 100): # for c in range(1,100): # r = r + i * a +b + c # e = time.time() # return r, e-s # q,w = n2(10000000000) # print(q, w) # def fn(s1, s2): # # list1 =list(s1) # list2 =list(s2) # list1.sort() # list2.sort() # # str1 = " ".join(list1) # str2 = " ".join(list2) # return str1 == str2 # return list1,list2 # s1 = "he1adrt" # s2 = "eard1th" # print(fn(s1,s2)) # lst1 = [0] * 26 # str1 ="bpple" # for i in str1: # idx = ord(i) - ord("a") # count = lst1[idx] # count +=1 # lst1[idx] = count # print(lst1) # #两个列表,分解成最小的,进行合并,通过算法,求出组合, #求出abc的组合,给a进行赋值, # import time # # strat =time.time() # for a in range(1001): # for b in range (1001): # c =1000-a-b # if a**2+b**2==c**2 # print(a,b,c) #排序的复杂度,最优时间复杂度, ''' 2.时间复杂度规则:常顺环,分高坏 基本操作:只有常数项,认为为o 顺序结构按照加法, 循环结构按照乘法, 分支结构取最大值, 判断一个算法的效率时候,只关注最高项 没有特殊说明都分析的是最坏的时间复杂时 ''' #冒泡排序 # def bubble(list1): # for i in range(len(list1) - 1): # for j in range(len(list1) - 1 -i): # if list1[j] > list1[j+1]: # list1[j],list1[j+1] = list1[j+1],list1[j] # return list1 # n = [8, 5, 6, 9, 99, 12, 75] # print(bubble(n)) # #冒泡的效率是最低的 # def bubble(list1): # exchanges = True #是否需要交换 # passnum = len(list1) - 1 # while passnum > 0 and exchanges: # # for i in range(passnum): # if list1[i] > list1[i+1]: # exchanges = True # # list1[i],list1[i+1] = list1[i+1],list1[i] # passnum = passnum - 1 # return list1 # n = [8, 5, 6, 9, 99, 12, 75] # print(bubble(n)) # #冒泡的效率是最低的 # def bubble(list1): # # p = len(list1) - 1 # while p > 0 : # for i in range(p): # if list1[i] > list1[i+1]: # list1[i], list1[i+1] = list1[i+1], list1[i] # # p -=1 # # return list1 # # n = [8, 5, 6, 9, 99, 12, 75] # print(bubble(n)) #选择排序,效率低下的原因,每次遍历 # # def bubble(list1): # # for i in range(len(list1) - 1): # for j in range: # if list1[i] > list1[j]: # list1[i], list1[j] = list1[j], list1[i] # j -=1 # return list1 # n = [8, 5, 6, 9, 99, 12, 75] # print(bubble(n)) #冒泡的效率是最低的 # def select_sort(alist): # n = len(alist) # for i in range(n - 1): # for j in range(i + 1, n): # if alist[i] > alist[j]: # alist.append[j]: # # return alist # n = [8, 5, 6, 9, 99, 12, 75] # print(select_sort(n)) # def insert_sort(alist): # for i in range(0,len(alist)): # current = alist[i] # preIndex = i - 1 # # while preIndex >= 0 and alist[preIndex] > current: # alist[preIndex + 1] = alist[preIndex] # alist[preIndex] = current # # preIndex -= 1 # # return alist # # # # n = [77,8, 5, 6, 9, 99, 12, 54,75] # print(insert_sort(n)) # def insertsort(n): # for i in range(0, len(n)): # c = n[i] # p = i - 1 # while p >= 0 and n[p] < c: # n[p + 1] =n[p] # n[p] = c # p -= 1 # return n # n = [77,8, 5, 6, 9, 99, 12, 54,75] # print(insertsort(n)) # def insert_sort(alist): # for i in range(0,len(alist)): # current = alist[i] # preIndex = i - 1 # # while preIndex >= 0 and alist[preIndex] > current: # alist[preIndex + 1] = alist[preIndex] # alist[preIndex] = current # # preIndex -= 1 # # return alist # n = [77,8, 5, 6, 9, 99, 12, 54,75] # print(insert_sort(n)) def f(n): n = len(n) for i in range(n - 1): for j in range (len(n) - 1 - i): if n[i] > n[j]: n[i] , n[j] = n[j] , n[i] return n n = [77,8, 5, 6, 9, 99, 12, 54,75] print(f(n)) # def select_sort(alist): # n = len(alist) # for i in range(n - 1): # for j in range(i + 1, n): # if alist[i] > alist[j]: # alist.append[j]: # # return alist # n = [8, 5, 6, 9, 99, 12, 75] # print(select_sort(n))
08-08
305
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)