complexityheapssorting
Need the top k without sorting everything?
Anonymous
for the k largest values, a min-heap of size k is handy when k is much smaller than n. keep the smallest current top value at the root; replace it only when a larger value arrives. heap work is about n log k, with k extra space; sorting all n values takes n log n work.
0