News for hackers. Stuff that matters.
Re:Anagram Scoring by geocar on Tuesday February 21, 2017 @03:04PM (#13699323)
The observation that two words are anagrams if they contain all the same letters isn't a huge leap from two words that contain all the same letters are anagrams.

That being said: Sorting isn't required. (An O(kn) solution exists to beat your O(nk log k))

Re:Anagram Scoring by lisper on Tuesday February 21, 2017 @03:51PM (#13699811)
It's not entirely clear that the O(nk) solution actually wins because k is small in this case, and so log(k) is very small. Unless you do some careful bit packing, the O(nk) algorithm will use more memory, which could make it slower because of increased cache misses.
Re:Anagram Scoring by geocar on Wednesday February 22, 2017 @12:46AM (#13703336)
The O(nk) solution has fewer branches (O(1) versus O(log k)), and even if you use a naïve implementation, O(nk) will still fit into L1 (assuming n is bigger than k).
Re:Anagram Scoring by lisper on Wednesday February 22, 2017 @01:38AM (#13703568)
> O(nk) will still fit into L1

Not even close. The lexicon is 230k words, times 26 letters is nearly 6MB. The i7 has a 64kB L1 cache and a 246kB L2 cache.

I think the only way to resolve this would be to actually do the experiment. I wouldn't bet my life savings on the outcome either way, but I'll give you even odds at low stakes that sorting is faster.

Re:Anagram Scoring by eridius on Tuesday February 21, 2017 @03:25PM (#13699534)
Yeah, there are other solutions, such as allocating an array of 26 letters (ignoring accents) and using that to count, but sorting is easy and is generally fast enough.
Re:Anagram Scoring by wrsh07 on Tuesday February 21, 2017 @06:53PM (#13701433)
Sorting is O(kn) where k is the largest number. (See radix sorting)
Re:Anagram Scoring by geocar on Wednesday February 22, 2017 @01:00AM (#13703390)
It also requires O(k) memory which is certainly larger than L1, causing two stalls instead of one.
Re:Anagram Scoring by wrsh07 on Wednesday February 22, 2017 @11:22AM (#13706620)
In this case, k is 26. Or ASCII value of 'z'. Either way, very cheap.
Re:Anagram Scoring by ouid on Tuesday February 21, 2017 @05:20PM (#13700613)
Map a-z to the first 26 primes, multiply and then quicksort your dictionary.
Re:Anagram Scoring by geocar on Wednesday February 22, 2017 @01:09AM (#13703434)
Limited to around 11 character words/phrases. You can do better.
Re:Anagram Scoring by ouid on Friday February 24, 2017 @02:53PM (#13727154)
yeah, there's no reason to sort, you can just hash. but I'm not sure I can improve the asymptotic complexity of a comparison