I have written an article on a new way of non comparison sorting and I want it to get peer reviewed. The sorting algorithm uses Trie data structure to store digits of numbers as nodes and iterating over each node 10 times (k) in a depth first traversal returns the sorted list. Time complexity O(n log m), log to the base k given a list of 'n' numbers in base 'k' and 'm' digits in the highest number in the list. 'm' doesn't depend on 'n'. Kinda like radix sort but still different.
I'm pretty sure the solution can be improved a lot or not be legit at all, hence I am trying to reach out to people who can help me on this. Can you please take a look at it and give me your feedback?
import collections class TrieNode: def __init__(self): self.children = collections.defaultdict(TrieNode) self.count = 0 class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): current = self.root for w in word: current = current.children[w] current.count += 1 class TrieSort: def __init__(self): self.res = [] self.max_num_len = 0 def buildTrie(self, nums): trie = Trie() self.max_num_len = len(str(max(nums))) # We get the highest number's max value of digits for num in nums: num_len = len(str(num)) zero_len = self.max_num_len - num_len trie.insert('0'* zero_len + str(num)) return trie.root def trieSort(self, node, word, depth): for i in range(0, 10): i = str(i) if not node.children.get(i): continue if depth == self.max_num_len-1: self.res += [int(word+i)] * node.children.get(i).count self.trieSort(node.children.get(i), word + i, depth +1) def sort(self, nums): trie = self.buildTrie(nums) # we start at root node with an empty word and at depth zero self.trieSort(trie, "", 0) return self.res ts = TrieSort() ts.sort([113, 20, 3999, 4])
Medium Article : https://medium.com/@jo07/triesort-in-linear-time-be1d07d41679
Github repo : https://github.com/jo07/Trie-Sort
Jo,
Cheers.