defquicksort(arr): iflen(arr) <= 1: return arr pivot = arr[len(arr) // 2] left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quicksort(left) + middle + quicksort(right)
function Dijkstra(Graph, source): dist[source] := 0 // 从源节点到自身的距离为0 for each vertex v in Graph: if v ≠ source: dist[v] := infinity // 其他节点的距离初始化为无穷大 unset vertices in priority queue while priority queue is not empty: u := vertex in priority queue with smallest distance remove u from priority queue for each neighbor v of u: alt := dist[u] + length(u, v) if alt < dist[v]: // 找到更短的路径 dist[v] := alt
defselection_sort(arr): n = len(arr) for i inrange(n): min_idx = i for j inrange(i+1, n): if arr[j] < arr[min_idx]: min_idx = j arr[i], arr[min_idx] = arr[min_idx], arr[i] # 交换 return arr
defquick_sort(arr): iflen(arr) <= 1: return arr pivot = arr[len(arr) // 2] # 选择基准值 left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quick_sort(left) + middle + quick_sort(right)
defdfs(graph, v, visited): visited[v] = True print(v, end=' ') for i inrange(len(graph[v])): if graph[v][i] == 1andnot visited[i]: dfs(graph, i, visited)