Beispiel #1
0
def radix_sort(iterable, reverse=False, radix=10):
    """
    Return a new sorted list from the integers in iterable, using radix sort.
    Integers can be both positive and negative.
    :param iterable:
    :param reverse:
    :param radix:
    :return:
    """
    const = radix * 2 - 1

    if reverse:
        shift = dict(zip(range(const), range(const - 1, -1, -1)))
    else:
        shift = dict(zip(range(const), range(const)))

    offset = radix - 1

    array = LogList(iterable)
    for x in range(radix + 1):
        buckets = [LogList([]) for _ in range(radix + offset)]
        for number in array:
            num = int((number / radix**x) % radix)
            if not less(number, 0):
                buckets[shift[num + offset]].append(number)
            else:
                buckets[shift[num]].append(number)
        array = LogList([])
        for section in [bucket for bucket in buckets if bucket]:
            array.extend(section)
        if len([bucket for bucket in buckets if bucket]) == 1:
            break
    return array
 def key(x, y):
     return less(y, x)
 def test_less_again(self):
     for x in self.array:
         less(x, 1)
     self.assertEqual(5, less.calls)