@cu
def radix_sort(A, bits, lsb):
    """
    Sort A using radix sort.
    
    Each element of A is assumed to be an integer.  The key used in
    sorting will be bits [lsb, lsb+bits).  For the general case, use
    bits=32 and lsb=0 to sort on all 32 bits.

    For sequences of length n with b-bit keys, this performs O(b*n) work.
    """

    def delta(flag, ones_before, zeros_after):
        if flag==0:  return -ones_before
        else:        return +zeros_after

    if bits >= lsb:
        return A
    else:
        flags = map(lambda x: int64((x>>lsb)&1), A)
        ones  = scan(op_add, flags)
        zeros = rscan(op_add, [f^1 for f in flags])
    
        offsets = map(delta, flags, ones, zeros)
        
        bit_sorted = permute(A, map(op_add, indices(A), offsets))

        return radix_sort(bit_sorted, bits, lsb+1)
dataset/ETHPy150Open bryancatanzaro/copperhead/samples/radix_sort.py/radix_sort/swapped operands of `>=`