数据结构 冒泡排序 二分法
def check_valid(s): stack = [] mapping = {')': '(', ']': '[', '}': '{'} for c in s: print(c) if c not in mapping: print(c not in mapping) stack.append(c) print(stack) # 如果列表为空或者字典中的元素不等于删除的最后一个元素 elif not stack or mapping[c] != stack.pop(): print(12) return False return not stack ret = check_valid('[)]',) print(ret)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
lst = [1, 56, 4, 898, 45, 133, 1, 2, 4, 1, 56] count = 0 while count < len(lst): # 控制次数 i = 0 while i < len(lst)-1: if lst[i] > lst[i+1]: lst[i], lst[i+1] = lst[i+1], lst[i] i += 1 count += 1 print(lst)
二分法 :
二分查找,每次能够排除掉一半的数据 ,查找的效率非常高,但是局限性比较大,必须是有序序列才可以使用二分查找.
要求是 查找序列必须是有序序列
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
lst = [22, 33, 44, 55, 66, 77, 88, 99, 101, 238, 345, 456, 567, 678, 789] def func(n,left,right): if left <= right: mid = (left+right)//2 if n > lst[mid]: left = mid+1 elif n < lst[mid]: right = mid - 1 elif n == lst[mid]: print("在这里") return mid return func(n, left, right) else: print("不在这里") return -1 print(func(66,0,len(lst)-1))