I wrote this counting sort implementation. It's a bit different than the standard one…
- User
I wrote this counting sort implementation. It's a bit different than the standard one since I"m not going backward but rather forward. Is there any disadvantage to my approach? Are there any bugs in my code?
codedef count_sort(values, key_fn): n_keys = max(key_fn(v) for v in values) + 1 if len(values) == 1: return values, [0] counts = [0] * n_keys for v in values: counts[key_fn(v)] += 1 cumcounts = [0] * n_keys for k in range(1, n_keys): cumcounts[k] = cumcounts[k-1] + counts[k-1] # permutation[i] = j means that in position i we want to put item j permutation = [None] * len(values) for v in values: k = key_fn(v) permutation[cumcounts[k]] = v cumcounts[k] += 1 return permutation - Assistant