- 博客(8)
- 收藏
- 关注
原创 二分查找(Python必备)
def binary_search(data, target, low, high): #适用于从小到大顺序排列的数据 if low>high: return False else: mid = (low+high)//2 if data[mid] > target: return binary_search(data, target, low, mid-1) elif data[m.
2022-05-13 10:46:02
239
原创 狄克斯特拉算法
graph = {}costs = {}parents = {}processed = []def find_lowest_cost_node(costs): lowest_cost = float("inf") lowest_cost_node = None for node in costs: #遍历所有的节点 cost = costs(node) if cost < lowest_cost and node not i.
2022-05-13 10:12:13
161
原创 必备排序算法--快速排序
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 ..
2022-04-25 12:06:32
658
原创 问题:找出列表中前K大的数(python实现)
#问题:取出列表中前k大的数def sift(li, low, high): #小根堆 # li指的是列表 # low指的是堆的根节点位置 # high指的是堆的最后一个元素的位置(作用值在于判断是否越界) i = low #i最开始指向根节点 j = 2*i + 1 #j开始是左孩子节点 tmp = li[low] #把堆存起来 while j <= high : #只要j位置有数 if j+1 <.
2022-04-15 18:08:39
1548
原创 Python中三个基础排序方式(冒泡排序,选择排序,插入排序)
冒泡排序:import randomdef bubble_sort(li): #复杂度O(n**2) for i in range(len(li)-1): #第i趟 exchange = False for j in range(len(li)-i-1): if li[j] > li[j+1]: li[j] , li[j+1]=li[j+1] , li[j]
2022-04-14 15:15:57
692
原创 小飞机大战外星人的小游戏的全部源代码(硬肝) PS:各模块名和代码中需要的图片放在了文章最后,方便各位学习。
import pygamefrom pygame.sprite import Spriteclass Alien(Sprite) : #表示单个外星人的类 def __init__(self,ai_game): #初始化外星人并设置其起始位置。 super().__init__() self.screen = ai_game.screen self.settings = ai_game.settings ..
2022-04-05 18:14:39
4062
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人