1. #define LeftChild(i) (2*(i)+1) void PercDown(vector<int>&num, int i, int n) { int child; int tmp; for (tmp = num[i]; LeftChild(i) < n; i = child) { child = LeftChild(i); if (child != n - 1 && num[child + 1] > num[child]) child++
import random import time from heapq import heappush, heappop def heapsort(iterable): h = [] for value in iterable: heappush(h, value) return [heappop(h) for i in range(len(h))] if __name__=="__main__": time_start = time.time() array = [random.r
用python写了个快排,第一次发现python居然可以这么简洁. def quicksort(arr): if len(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) + midd