Python二分法查找
1.1二分前提是有序,,否则不可以2分,2分查找的时间复杂度是O(log n);排序后二分查找到适当的位置插入数值
lst = [37,99,73,48,47,40,40,25,99,51] def insert_sort(orderlist,i): low = 0 high = len(orderlist) #10 while low < high: mid = (low + high) // 2 print('mid={}'.format(mid)) if i < orderlist[mid]: high = mid else: low = mid + 1 # low = mid print('low={}'.format(low)) print(low) orderlist.insert(low,i) print(orderlist) insert_sort(sorted(lst),100)
本文为原创文章,转载请标明出处