top k

If you want to find the top k data from the massive data.

Then use a small top heap with the number of elements k. In this case, the element at the top of the heap is the smallest of the k numbers, and only this element may not be the largest in top k.

A new element is compared with the top of the heap. If it is smaller than it, it is definitely not an element of top k, and it is thrown away. If it is larger than it, then the top of the heap is definitely not an element in top k at this time, and the two are exchanged. Then save the heap order again.

When all the data has been traversed, the elements of top k are selected.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324759096&siteId=291194637