递归
def func(arg1,arg2,stop): if arg1==0: print(arg1,arg2) arg3=arg1+arg2 print(arg3) if arg3<stop: func(arg2,arg3,stop) func(0,1,30)
二分查找
#!/usr/bin/env python # -*- coding: UTF-8 -*- def binary_search(data_source,find_n): mid=int(len(data_source)/2) if len(data_source) >=1: if data_source[mid] > find_n: print(data_source[mid]) binary_search(data_source[:mid],find_n) elif data_source[mid] < find_n: print(data_source[mid]) binary_search(data_source[mid:],find_n) else: print("found ",data_source[mid]) else: print("cannot found") if __name__=='__main__': data=list(range(1,1000000)) binary_search(data,1)
多维数组交叉
array = [[col for col in range(4)] for row in range(4)] # 初始化一个4*4数组 # array=[[col for col in 'abcd'] for row in range(4)] for row in array: # 旋转前先看看数组长啥样 print(row) for i, row in enumerate(array): for index in range(i, len(row)): tmp = array[index][i] # 将每一列数据在每一次遍历前,临时存储 array[index][i] = array[i][index] # 将每一次遍历行的值,赋值给交叉的列 print(tmp, array[i][index]) # = tmp array[i][index] = tmp # 将之前保存的交叉列的值,赋值给交叉行的对应值 for r in array: # 打印每次交换后的值 print(r)
冒泡排序
li=[10,4,33,21,54,3,8,11,5,22,2,1,17,13,6] for j in range(1,len(li)): for i in range(len(li)-j): if li[i] >li[i+1]: tmp=li[i+1] li[i + 1]=li[i] li[i]=tmp print(li)