排序 插入、希尔、冒泡、选择
def insertSort(arr):
for i in range(1,len(arr)):
tmp = arr[i]
for j in range(i-1,-1,-1):
if arr[j] <= tmp:
arr[j+1] = tmp
break
else:
arr[j+1] = arr[j]
return arr
def shellSort(arr):
d = len(arr)//2
while d>=1:
for i in range(d,len(arr)):
tmp = arr[i]
for j in range(i-d,-1,-1):
if arr[j] <= tmp:
arr[j+d] = tmp
break
else:
arr[j+d] = arr[j]
d=d//2
return arr
def bubbleSort(arr):
for i in range(len(arr)):
for j in range(i+1,len(arr)):
if arr[i] > arr[j]:
arr[i],arr[j] = arr[j],arr[i]
return arr
def selectSort(arr):
for i in range(len(arr)-2):
min_ = arr[i]
min_i = i
for j in range(i+1,len(arr)):
if arr[j] < min_:
min_ = arr[j]
min_i = j
arr[i],arr[min_i] = arr[min_i],arr[i]
return arr
排序 快速排序及应用
def QuickSort(arr,start,end,k=6):
if start >= end:
return
i,j = start,end
base = arr[i]
while i < j:
while i < j and arr[j] >= base:
j -=1
arr[i] = arr[j]
while i < j and arr[i] <= base:
i +=1
arr[j] = arr[i]
arr[i] = base
QuickSort(arr, start, i - 1)
QuickSort(arr, j + 1, end)
#应用 找乱序数组中第K大的数字(也可用最大堆) o(nlogm):
def FindKth(arr,start,end,k=6):
if start >= end:
return
i,j = start,end
base = arr[i]
while i < j:
while i < j and arr[j] >= base:
j -=1
arr[i] = arr[j]
while i < j and arr[i] <= base:
i +=1
arr[j] = arr[i]
arr[i] = base
#此时 i==j
if j == k:
return
elif k<j: #要找的第K大数在左半边
FindKth(arr, start, i - 1)
elif k>j:
FindKth(arr, j + 1, end)
return arr[k]
归并排序
#归并排序模板
def merge(nums,left,right):
mid = (left+right)//2
if left<right:
merge(nums,left,mid)
merge(nums,mid+1,right)
mergeSort(nums,left,mid,right)
def mergeSort(nums,left,mid,right):#[left,mid][mid+1,right]
i=left;j=mid+1
temp = []
while i<=mid and j<= right:
if nums[i]<nums[j]:
temp.append(nums[i])
i+=1
else:
temp.append(nums[j])
j+=1
if i<= mid:
temp.extend(nums[i:mid+1])
if j<=right:
temp.extend(nums[j:right+1])
for i in range(len(temp)):
nums[left+i] = temp[i]
并查集模板
#init
n = 6
parent = [-1]*n
rank = [0]*n
edges = [(0,1),(1,2),(2,3),(1,4),(2,5)]
def find(x,parent):
while parent[x] != -1:
x = parent[x]
return x
def union(x,y,parent,rank):
x_root = find(x,parent)
y_root = find(y,parent)
if x_root == y_root:
return 0
else:
if rank[x_root] > rank[y_root]:
parent[y_root] = x_root
elif rank[x_root] < rank[y_root]:
parent[x_root] = y_root
else:
parent[x_root] = y_root
rank[y_root] += 1
return 1
for i in range(n-1):
if union(edges[i][0],edges[i][1],parent,rank) == 0:
print('有环')
else:
print('五环')
二分查找模板(check需要自己完成 返回true or false)
def bsearch(nums):
l = 0;r=len(nums)-1
while l<r:
mid = (l+r)>>1#[l,mid][mid+1,r]
if check(mid):
r = mid
else:
l = mid + 1
def bserach(nums):
l = 0;r=len(nums)-1
while l<r:
mid = (l+r+1)>>1#[l,mid-1][mid,r]
if check(mid):
r = mid-1
else:
l = mid
堆排序模板
def heapify(arr, n, i):
largest = i
l = 2 * i + 1 # left = 2*i + 1
r = 2 * i + 2 # right = 2*i + 2
if l < n and arr[i] < arr[l]:
largest = l
if r < n and arr[largest] < arr[r]:
largest = r
if largest != i:
arr[i],arr[largest] = arr[largest],arr[i] # 交换
heapify(arr, n, largest)
def heapSort(arr):
n = len(arr)
# Build a maxheap.
for i in range(n, -1, -1):
heapify(arr, n, i)
# 一个个交换元素
for i in range(n-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i] # 交换
heapify(arr, i, 0)
arr = [ 12, 11, 13, 5, 6, 7]
heapSort(arr)
n = len(arr)
print ("排序后")
for i in range(n):
print ("%d" %arr[i])
桶排序
def RadixSort(a):
i = 0 #初始为个位排序
n = 1 #最小的位数置为1(包含0)
max_num = max(a) #得到带排序数组中最大数
while max_num > 10**n: #得到最大数是几位数
n += 1
while i < n:
bucket = {} #用字典构建桶
for x in range(10):
bucket.setdefault(x, []) #将每个桶置空
for x in a: #对每一位进行排序
radix =int((x / (10**i)) % 10) #得到每位的基数
bucket[radix].append(x) #将对应的数组元素加入到相应位基数的桶中
j = 0
for k in range(10):
if len(bucket[k]) != 0: #若桶不为空
for y in bucket[k]: #将该桶中每个元素
a[j] = y #放回到数组中
j += 1
i += 1
arr = [12,3,45,3543,214,1,4553]
RadixSort(arr)
print(arr)