标签:缺点 理解 imp append 交换 自己 hang none pre
跟随视频学习了一些常见的排序,把自己第二天能理解的记录下来,万一以后忘记了呢?
def linear_search(li, target): """ 线性查找 """ for ind, tar in enumerate(li): if tar == target: return ind else: return None def binary_search(li, target): """ 二分查找,注意mid+-1,既提高速度,又与while的=相照应,才能找到列表最后一个值 :param li:列表 :param target:要查找的元素 :return: 目标元素的下标 """ low = 0 high = len(li) - 1 while low <= high: print("low = %s, high = %s" % (low, high)) sleep(5) mid = (low + high) // 2 if li[mid] == target: return mid elif li[mid] > target: high = mid - 1 else: low = mid + 1 else: return None def Bubble_sort(li): """ 冒泡排序,change加入:若两次遍历无交换,则数列已经有序,故提前结束 :param li: :return: """ for i in range(len(li) - 1): change = None for j in range(len(li) - i - 1): if li[j] < li[j + 1]: li[j], li[j + 1] = li[j + 1], li[j] change = True if not change: break def Select_sort_simple(li): """ 选择排序,简单模式 缺点:需要两个列表,所占空间加倍 :param li: :return: """ li_tar = [] for i in range(len(li)): tmp = min(li) li_tar.append(tmp) li.remove(tmp) return li_tar def Select_sort(li): """ 选择排序,在一个数列中进行 :param li: :return: """ for i in range(len(li) - 1): min_loc = i for j in range(i + 1, len(li)): if li[j] < li[min_loc]: li[j], li[min_loc] = li[min_loc], li[j] def Insert_sort(li): """ 插入排序 :param li: :return: """ for i in range(1, len(li)): tmp = li[i] j = i - 1 while j >= 0 and li[j] > tmp: li[j+1] = li[j] j = j - 1 li[j+1] = tmp
标签:缺点 理解 imp append 交换 自己 hang none pre
原文地址:https://www.cnblogs.com/Bethuel/p/13377767.html