1
$\begingroup$

Let's say I have a long text of 1M words and I would like to create a table of all the words ordered by the number of occurrences in the text.

One approach would be populating a dynamic array with each word and linear search them to count the occurrences in $O(n^2)$ then sort the array by occurrences in $O(n \log n)$.

Another approach would be to use y priority queue and a trie. The insertion in the priority queue is $O(\log n)$ and the build of the trie is $O(n)$. But traversing the trie to build the priority queue is somehow difficult to evaluate.

Eventually using a hash map seems to be the best solution, but computing the hash could cost a little bit of time even though it is just a constant. In this you have $n$ insertion/lookup in $O(1)$ then a final sort of the hashmap by occurrences in $O(n \log n)$.

So it is clear that the former approach is the worse and the latter the best. But how can I evaluate the complexity of the second one?

$\endgroup$
1
$\begingroup$

I'm not sure I understand why do you need a priority queue.

The best algorithm should be as follows (pseudocode):

1) count the words
2) sort them by count

Counting each word you can loop through each word and update the count in the hash table for each word. This is done $n$ times and you need $O(1)$ for accessing + $O(1)$ updating the hash table thus $O(n)$.

Sorting is $O(n \log n)$. Thus the complexity is $O(n \log n)$.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.