标签:insert 介绍 时间 归并算法 color 数组 ble 空间 []
数据结构:
计算机存储,组织数据的结构。指相互之间存在着一种或多种关系的数据元素的集合和该集合中数据元素之间的关系组成.
线性数据结构:数组,链表 应用:队列 栈
非线性数据结构:树,图
算法:
对数据结构中数据的操作
low B三件套
#冒泡排序 #时间复杂度:O(n2) #空间复杂度:O(1) def bubble_sort(li): for i in range(len(li)-1): flag = False for j in range(len(li)-i-1): if li[j+1] < li[j]: li[j+1], li[j] = li[j], li[j+1] flag = True if not flag: return
# 选择排序 # 时间复杂度: O(n^2) # 空间复杂度: O(1) def select_sort(li): for i in range(len(li)-1): minLoc = i for j in range(i+1,len(li)): if li[j] < li[minLoc]: # li[2] < li[0] li[j], li[minLoc] = li[minLoc], li[j]
# 插入排序 # 时间复杂度: O(n^2) # 空间复杂度: O(1) def insert_sort(li): for i in range(1,len(li)): tmp = li[i] j = i - 1 while j >= 0 and tmp < li[j] : li[j+1] = li[j] j = j - 1 li[j+1] = tmp
N B三件套
# 快速排序 # 时间复杂度: O(nlogn) # 空间复杂度: O(1) def patition(li, left, right): tmp = li[left] # 5 while left < right: while left < right and tmp <= li[right]: right = right - 1 li[left] = li[right] # [2,7,4,6,3,1,2,9,8] # [2,1,4,6,3,1,2,9,8] while left < right and tmp >= li[left]: left = left + 1 li[right] = li[left] # [2,7,4,6,3,1,7,9,8] li[left] = tmp return left def quick_sort(li, left, right): if left < right: mid = patition(li, left, right) quick_sort(li, left, mid-1) quick_sort(li, mid+1, right)
# 归并算法 # 时间复杂度: O(nlogn) # 空间复杂度: O(n) def merge(li, left, mid, right): i = left j = mid + 1 ltmp = [] while i <= mid and j <= right: if li[i] < li[j]: ltmp.append(li[i]) i = i + 1 else: ltmp.append(li[j]) j = j + 1 while i <= mid: ltmp.append(li[i]) i = i + 1 while j <= right: ltmp.append(li[j]) j = j + 1 li[left:right+1] = ltmp def merge_sort(li, left, right): if left < right: mid = (left + right) // 2 merge_sort(li, left, mid) merge_sort(li, mid+1, right) merge(li, left, mid, right)
标签:insert 介绍 时间 归并算法 color 数组 ble 空间 []
原文地址:https://www.cnblogs.com/3sss-ss-s/p/10328311.html