Ejemplo n.º 1
0
def main():
    "Tests the sort."
    try:
        size = int(sys.argv[1].strip())
    except IndexError:
        size = 6
    sort_test([radix_sort, sorted], max_size_order=size)
Ejemplo n.º 2
0
        lm, rm = start, end - 1

        while lm < rm:
            while lm < rm and nos[rm] > pivot:
                rm -= 1
            while lm < rm and nos[lm] <= pivot:
                lm += 1

            nos[lm], nos[rm] = nos[rm], nos[lm]

            if nos[lm] == pivot:
                key = lm

        nos[key], nos[lm] = nos[lm], nos[key]

        partial(start, lm)
        partial(lm + 1, end)

    partial(0, len(nos))

    return nos


if __name__ == '__main__':
    from sort_test import sort_test

    sort_test(quick_sort)
    sort_test(quick_sort2)


Ejemplo n.º 3
0
def selection_sort(nos):
    for j in range(len(nos) - 1, 0, -1):
        mi = 0

        for i in range(1, j + 1):
            if nos[i] > nos[mi]:
                mi = i

        nos[j], nos[mi] = nos[mi], nos[j]

    return nos


if __name__ == '__main__':
    from sort_test import sort_test

    sort_test(selection_sort)


Ejemplo n.º 4
0
def insertion_sort(nos):
    for i in range(1, len(nos)):

        inserted = nos[i]
        j = i - 1

        while j >= 0 and nos[j] > inserted:
            nos[j + 1] = nos[j]
            j -= 1

        nos[j + 1] = inserted

    return nos


if __name__ == '__main__':
    from sort_test import sort_test

    sort_test(insertion_sort)
Ejemplo n.º 5
0
        else:
            return False

    i = 0
    j = len(nos) - 1

    while i < j:
        any_swapped = False

        for si in range(i, j):
            swapped = swap(si)

            if swapped:
                if not any_swapped and si > 0:
                    i = si - 1

                any_swapped = True

        j -= 1

        if not any_swapped:
            return nos

    return nos


if __name__ == '__main__':
    from sort_test import sort_test

    sort_test(buble_sort)
Ejemplo n.º 6
0
        while j < len(left) or k < len(right):
            if j == len(left) or (k < len(right) and left[j] > right[k]):
                nos[i] = right[k]
                k += 1
            else:
                nos[i] = left[j]
                j += 1

            i += 1

    def merge_sort_util(start, end):
        if end - start <= 1:
            return
        else:
            pivot = start + (end - start) // 2

            merge_sort_util(start, pivot)
            merge_sort_util(pivot, end)

            merge(start, pivot, end)

    merge_sort_util(0, len(nos))

    return nos


if __name__ == '__main__':
    from sort_test import sort_test

    sort_test(merge_sort, nos=[])
Ejemplo n.º 7
0
def test(sort_func):
    return sort_test(sort_func, nos=nos, repeat=1)