def binary_search(m_list,item):
low = 0
high = len(m_list)-1
while low <= high:
mid = int((low + high) / 2)
guess = m_list[mid]
if guess == item:
return mid
if item > guess:
low = mid + 1
else:
high = mid-1
return None
my_list = [1,3,5,7,9]
a=binary_search(my_list,3)
print (a)
mid需要使用int转化为int类型,否则list[mid] 计算的时候会出现浮点数,导致程序的失败
改进一下
def binary_search(m_list,item):
low = 0
high = len(m_list)-1
while low <= high:
mid = int((low + high) / 2)
guess = m_list[mid]
if guess == item:
return mid
if item > guess:
low = mid + 1
else:
high = mid-1
return None
my_list = [1,3,5,7,9]
#a=binary_search(my_list,3)
#print (a)
print (binary_search(my_list,9))
print ()打上括号
def findSmallest(arr): smallest = arr[0] smallest_index = 0 for i in range(1,len(arr)): if arr[i] < smallest: smallest = arr[i] smallest_index = i return smallest_index def selectionSort(arr): newArr = [] for i in range(len(arr)): smallest = findSmallest(arr) print ("the smallest is ",smallest) newArr.append(arr.pop(smallest)) return newArr print (selectionSort([5,3,6,2,10]))
简化版:
arr = [5,3,7,28,33,66] print (arr) arrTmp = [] small = arr[0] for i in range(len(arr)): small = arr[0] for j in range(1,len(arr)): if arr[j] < small: small = arr[i] print("small is ",small) arrTmp.append(small) print(arrTmp)
快排
#include
#include using namespace std; int arry[] = {1,52,53,64,22,23}; void select(int left,int right) { if(left > right) return; int base = arry[left]; int temp; int i = left, j = right; //cout<<"you are right"< = base && i < j) j--; while(arry[i] <= base && i < j) i++; if(i < j){ temp = arry[i]; arry[i] = arry[j]; arry[j] = temp; } } arry[left] = arry[i]; arry[i] = base; for(int k = 0;k < 6;k++) { cout< 修改快排
void select(int left,int right,int *arry) { if(left > right) return; int i = left; int j = right; int base = arry[left]; int temp = 0; while( i != j) { //从右开始遍历 while(arry[j] >=base && i < j) j--; while(arry[i] <= base && i< j) i++; if(i < j){ temp = arry[i]; arry[i] = arry[j]; arry[j] = temp; } } arry[left] = arry[i]; arry[i] = base; select(left,i-1,arry); select(i+1,right,arry); } int main() { int arry[100] = {1,5,2,3,4,8,9,10}; select(0,8,arry); for(int i = 0;i < 8;i++) { printf(" %d",arry[i]); } /* Write C code in this online editor and run it. */ printf("Hello, World! \n"); return 0; }
Python版
#!/usr/bin/python # Write Python 3 code in this online editor and run it. def select(arry): print("arry:",arry) if len(arry) < 2: return arry else: base = arry[0] less = [i for i in arry[1:] if i <= base] greater = [i for i in arry[1:] if i > base] return select(less)+[base]+select(greater) print (select([10,5,2,6,342,75,23]))
c和Python快排的区别,在于c中的数组没有变,在递归使用的是同一个数组,只是在改变数组中值的位置,而Python在递归中直接将数组进行切分和在重组
查找数组中最大的两个数字
可以查找出两个最大的数字,又可以先进行排序,在按顺序输出数组最大的两位数
#!/usr/bin/python # Write Python 3 code in this online editor and run it. def select(arry): max_num = arry[0]; second_num = 0; for i in range(1,len(arry)): if max_num < arry[i]: second_num = max_num max_num = arry[i] elif second_num < arry[i]: second_num = arry[i] return [max_num]+[second_num] print (select([12,34,632,23]))
广度优先搜索:
小结: 1、graph中的输入怎样实现入队操作 graph中查询到的值可以直接赋值给deque() # Write Python 3 code in this online editor and run it. from collections import deque friend_graph = {} friend_graph["li yong"] = ["luo ting","zhang san","li si"] friend_graph["luo ting"] = ["luo yao","xiao zhi","bo zai"] friend_graph["zhang san"] = [] friend_graph["li is"] = [] friend_graph["luo yao"] = [] friend_graph["xiao zhi"] = [] friend_graph["bo zai"] = [] print(friend_graph["li yong"]) def search(name): search_name = deque() search_name += friend_graph[name] //这里如果为"="可以完成赋值,左边变为list类型,不在具有deque的特性 //为“+=”的时候保留左边为deque friend_name = search_name.popleft() print(friend_name) print(search_name) search("li yong") Python的语句可以直接判断函数的返回值 注意各种封装容器的赋值操作 list赋值给deque使用+= 数组自己的添加成员需要使用对应方法.append() from collections import deque friend_graph = {} friend_graph["li yong"] = ["luo ting","zhang san","li si"] friend_graph["luo ting"] = ["luo yao","xiao zhi","bo zai"] friend_graph["zhang san"] = [] friend_graph["li si"] = [] friend_graph["luo yao"] = [] friend_graph["xiao zhi"] = [] friend_graph["bo zai"] = [] print(friend_graph["li yong"]) def check(name): return name == "xiao zhi" #return Ture def search(name): search_name = deque() search_name += friend_graph[name] searched = [] while search_name: friend_name = search_name.popleft() if not friend_name in searched: if check(friend_name): print("the person showup") return True else: searched.append(friend_name) search_name += friend_graph[friend_name] return False print(search("li yong")) 1、首先是存储键值对的list表格 2、然后是存储list表格的deque()数组,在deque中一个个将输出出队列 3、其次是存储已经进行比较过的search数组,在searched中将数据一个个的添加进来 判断条件if not name in searched: 进行一个数组中数据的判断
完整代码 from collections import deque graph = {} graph["li"] = ["liu","guang","zhuang"] graph["liu"] = ["zhao","qian","sun","li"] graph["guang"] = ["bai","he","da"] graph["zhuang"] = [] graph["zhao"] = [] graph["qian"] = [] graph["sun"] = [] graph["bai"] = [] graph["he"] = [] graph["da"] = [] #print(graph["liu"]) def person_is_seller(name): print ("helo") print (name) return name[-1] == 'a' def search(name): search_name = deque() search_name += graph[name] print("search_name is",search_name) searched = [] while search_name: person = search_name.popleft() print("person is ",person) if not person in searched: if person_is_seller(person): print ("is a da jiang jun",person) return True else: search_name += graph[person] print("else search_name:",search_name) searched.append(person) return False search("li")
迪克斯算法
********************************** 迪克斯算法: #把每个点制作为散列表 graph = {} graph["start"] = [] graph["start"]["a"] = 6 graph["start"]["b"] = 2 graph["a"] = {} graph["a"]["fin"] = 1 graph["b"] = {} graph["b"]["a"] = 3 graph["b"]["fin"] = 5 graph["fin"] = {} 创建开销表存储每个节点的开销 infinity = float("inf") infinity = float("inf") costs = {} costs["a"] = 6 costs["b"] = 2 const["fin"] = infinity 创建父节点的散列表 parents = {} parents["a"] = "start" parents["b"] = "start" parents["fin"] = None 最后使用数组存储记录处理过的节点 processed = [] //全局处理 node = find_lowest_cost_node(costs) //在未处理的节点中找出开销最小的节点 while node is not None: cost = costs[node] neighbors = graph[node] for n in neighbors.keys(): new_cost = cost + neighbors[n] if cossts[n] > new_cost: costs[n] = new_cost parents[n] = node processed.append(node) node = find_lowest_cost_node(costs) def find_lowest_cost_node(costs): lowest_cost = float("inf") lowest_cost_node = None for node in costs: cost = costs[node] if cost < lowest_cost and node not in processed: lowest_cost = cost lowest_cost_node = node return lowest_cost_node