Skip to content

Instantly share code, notes, and snippets.

@wesm
Created January 10, 2012 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesm/1590566 to your computer and use it in GitHub Desktop.
Save wesm/1590566 to your computer and use it in GitHub Desktop.
Stable O(max(n, k)) counting sort on n integers in range [0, ..., k - 1]
@cython.boundscheck(False)
@cython.wraparound(False)
def groupsort_indexer(ndarray[int32_t] index, Py_ssize_t ngroups):
cdef:
Py_ssize_t i, loc, label, n
ndarray[int32_t] counts, where, result
# count group sizes, location 0 for NA
counts = np.zeros(ngroups + 1, dtype='i4')
n = len(index)
for i from 0 <= i < n:
counts[index[i] + 1] += 1
# mark the start of each contiguous group of like-indexed data
where = np.zeros(ngroups + 1, dtype='i4')
for i from 1 <= i < ngroups + 1:
where[i] = where[i - 1] + counts[i - 1]
# this is our indexer
result = np.zeros(n, dtype='i4')
for i from 0 <= i < n:
label = index[i] + 1
result[where[label]] = i
where[label] += 1
return result, counts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment