Ejemplo n.º 1
0
def counting_sort(a, k):
    c = new_zero_array(k)
    for i in range(len(a)):
        c[a[i]] += 1
    for i in range(1, k):
        c[i] += c[i - 1]
    b = new_array(len(a))
    for i in range(len(a) - 1, -1, -1):
        c[a[i]] -= 1
        b[c[a[i]]] = a[i]
    return b
Ejemplo n.º 2
0
def counting_sort(a, k):
    c = new_zero_array(k)
    for i in range(len(a)):
        c[a[i]] += 1 
    for i in range(1, k):
        c[i] += c[i-1]  
    b = new_array(len(a))
    for i in range(len(a)-1, -1, -1):
        c[a[i]] -= 1        
        b[c[a[i]]] = a[i]   
    return b
Ejemplo n.º 3
0
def radix_sort(a):
    for p in range(w//d):
        c = new_zero_array(1<<d)
        b = new_array(len(a))
        for i in range(len(a)):
            bits = (a[i] >> d*p)&((1<<d)-1) 
            c[bits] += 1 
        for i in range(1, 1<<d):
            c[i] += c[i-1]
        for i in range(len(a)-1, -1, -1):
            bits = (a[i] >> d*p)&((1<<d)-1)
            c[bits] -=1
            b[c[bits]] = a[i] 
        a = b
    return b
Ejemplo n.º 4
0
def radix_sort(a):
    for p in range(w // d):
        c = new_zero_array(1 << d)
        b = new_array(len(a))
        for i in range(len(a)):
            bits = (a[i] >> d * p) & ((1 << d) - 1)
            c[bits] += 1
        for i in range(1, 1 << d):
            c[i] += c[i - 1]
        for i in range(len(a) - 1, -1, -1):
            bits = (a[i] >> d * p) & ((1 << d) - 1)
            c[bits] -= 1
            b[c[bits]] = a[i]
        a = b
    return b