排序法
AI小笔记
人一能之,己百之;人十能之,己千之。
展开
-
归并排序(Python)
# -*- coding:utf-8 -*-# @Time : 2020/9/17 9:45# @Author: JulyLi# @File : merge_sort.pydef merge(li, low, mid, high): i = low j = mid + 1 ltmp = [] while i <= mid and j <= high: # 只要左右两边都有数 if li[i] < li[j]:原创 2020-09-17 10:33:15 · 113 阅读 · 0 评论 -
堆排序(Python)
# -*- coding:utf-8 -*-# @Time : 2020/9/16 16:32# @Author: JulyLi# @File : heap_sort.pydef sift(li, low, high): """ :param li:列表 :param low:堆的根节点位置 :param high:堆的最后一个元素的位置 """ i = low # i最开始指向根节点 j = 2 * i + 1 # j开始是左孩子原创 2020-09-17 10:32:05 · 114 阅读 · 0 评论 -
快排(Python)
def partition(li, left, right): tmp = li[left] while left < right: while left < right and li[right] >= tmp: # 从右找比tmp小的数 right -= 1 # 往左走一步 li[left] = li[right] # 把右边值写到左边空位上 while left < right and原创 2020-09-16 16:04:23 · 101 阅读 · 0 评论 -
插入排序(Python)
def insertionSort(alist): for index in range(1, len(alist)): currentvalue = alist[index] position = index while position > 0 and alist[position - 1] > currentvalue: alist[position] = alist[position - 1]原创 2020-05-17 00:28:39 · 129 阅读 · 0 评论 -
选择排序(Python)
def selectionSort(alist): for fillslot in range(len(alist) - 1, 0, -1): positionOfMax = 0 for location in range(1, fillslot + 1): if alist[location] > alist[positionOfMax]: positionOfMax = location原创 2020-05-16 22:51:44 · 190 阅读 · 0 评论 -
冒泡排序(Python)
def bubbleSort(alist): for passnum in range(len(alist) - 1, 0, -1): for i in range(passnum): if alist[i] > alist[i + 1]: alist[i], alist[i + 1] = alist[i + 1], alist[i] return alist原创 2020-05-16 17:28:35 · 175 阅读 · 0 评论