def Partition(arr): sa=[] sb=[] index=random.randint(0,len(arr)-1) p=arr[index] for i in arr: sa.append(i) if i>p else sb.append(i) return (sa,sb) def TopK(arr,k): if k<=0: return [] if len(arr)<=k: return arr (sa,sb)=Partition(arr) return TopK(sa,k)+TopK(sb,k-len(sa))
时间: 2024-10-13 12:00:50