refactor and add the topK algorithm