# æåº ## 常èæåº ### å¿«éæåº ```Python import random def partition(nums, left, right): if left >= right: return pivot_idx = random.randint(left, right) pivot = nums[pivot_idx] nums[right], nums[pivot_idx] = nums[pivot_idx], nums[right] partition_idx = left for i in range(left, right): if nums[i] < pivot: nums[partition_idx], nums[i] = nums[i], nums[partition_idx] partition_idx += 1 nums[right], nums[partition_idx] = nums[partition_idx], nums[right] partition(nums, partition_idx + 1, right) partition(nums, left, partition_idx - 1) return def quicksort(A): partition(A, 0, len(A) - 1) return A if __name__ == '__main__': a = [7, 6, 8, 5, 2, 1, 3, 4, 0, 9, 10] print(a) print(quicksort(a)) ``` ### å½å¹¶æåº ```Python def merge(A, B): C = [] i, j = 0, 0 while i < len(A) and j < len(B): if A[i] <= B[j]: C.append(A[i]) i += 1 else: C.append(B[j]) j += 1 if i < len(A): C += A[i:] if j < len(B): C += B[j:] return C def mergsort(A): n = len(A) if n < 2: return A[:] left = mergsort(A[:n // 2]) right = mergsort(A[n // 2:]) return merge(left, right) if __name__ == '__main__': a = [7, 6, 8, 5, 2, 1, 3, 4, 0, 9, 10] print(a) print(mergsort(a)) ``` ### å æåº ç¨æ°ç»è¡¨ç¤ºçå®ç¾äºåæ complete binary tree > å®ç¾äºåæ VS å ¶ä»äºåæ  [å¨ç»å±ç¤º](https://www.bilibili.com/video/av18980178/)  æ ¸å¿ä»£ç ```Python def heap_adjust(A, start=0, end=None): if end is None: end = len(A) while start is not None and start < end // 2: l, r = start * 2 + 1, start * 2 + 2 swap = None if A[l] > A[start]: swap = l if r < end and A[r] > A[start] and (swap is None or A[r] > A[l]): swap = r if swap is not None: A[start], A[swap] = A[swap], A[start] start = swap return def heapsort(A): # construct max heap n = len(A) for i in range(n // 2 - 1, -1, -1): heap_adjust(A, i) # sort for i in range(n - 1, 0, -1): A[0], A[i] = A[i], A[0] heap_adjust(A, end=i) return A # test if __name__ == '__main__': a = [7, 6, 8, 5, 2, 1, 3, 4, 0, 9, 10] print(a) print(heapsort(a)) ``` ## é¢ç® ### [kth-largest-element-in-an-array](https://leetcode-cn.com/problems/kth-largest-element-in-an-array/) - æè·¯ 1: sort åå第 k ä¸ªï¼æç®åç´æ¥ï¼å¤æåº¦ O(N log N) 代ç ç¥ - æè·¯ 2: ä½¿ç¨æå°å ï¼å¤æåº¦ O(N log k) ```Python class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: # note that in practice there is a more efficient python build-in function heapq.nlargest(k, nums) min_heap = [] for num in nums: if len(min_heap) < k: heapq.heappush(min_heap, num) else: if num > min_heap[0]: heapq.heappushpop(min_heap, num) return min_heap[0] ``` - æè·¯ 3: [Quick select](https://en.wikipedia.org/wiki/Quickselect)ï¼æ¹å¼ç±»ä¼¼äºå¿«æï¼æ¯æ¬¡ partition åæ£æ¥ pivot æ¯å¦ä¸ºç¬¬ k 个å ç´ ï¼å¦ææ¯åç´æ¥è¿åï¼å¦ææ¯ k 大ï¼åç»§ç» partition å°äº pivot çå ç´ ï¼å¦ææ¯ k å°åç»§ç» partition å¤§äº pivot çå ç´ ãç¸è¾äºå¿«æï¼quick select æ¯æ¬¡åªé partition ä¸ä¾§ï¼å æ¤å¹³åå¤æåº¦ä¸º O(N)ã ```Python class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: k -= 1 # 0-based index def partition(left, right): pivot_idx = random.randint(left, right) pivot = nums[pivot_idx] nums[right], nums[pivot_idx] = nums[pivot_idx], nums[right] partition_idx = left for i in range(left, right): if nums[i] > pivot: nums[partition_idx], nums[i] = nums[i], nums[partition_idx] partition_idx += 1 nums[right], nums[partition_idx] = nums[partition_idx], nums[right] return partition_idx left, right = 0, len(nums) - 1 while True: partition_idx = partition(left, right) if partition_idx == k: return nums[k] elif partition_idx < k: left = partition_idx + 1 else: right = partition_idx - 1 ``` ## åè [å大ç»å ¸æåº](https://www.cnblogs.com/onepixel/p/7674659.html) [äºåå ](https://labuladong.gitbook.io/algo/shu-ju-jie-gou-xi-lie/er-cha-dui-xiang-jie-shi-xian-you-xian-ji-dui-lie) ## ç»ä¹ - [ ] æåå¿«æãå½å¹¶ãå æåº