Ejemplo n.º 1
0
def sort_test(sort_func, no_count = 16, no_ext = 300, nos = None, repeat = 1000):
    def sort_copy(src):
        global last_sorted
        last_sorted = sort_func(copy_list(src))

    print('-'*32, sort_func.__name__, 'begin', '-'*32)
    print()

    unsorted = randint_list(no_count, no_ext = no_ext) if nos is None else nos
    asc_list = sorted(unsorted)
    times = []

    print_list('unsorted:', unsorted)
    t1 = timeit(lambda: sort_copy(unsorted), number = repeat)
    print_list('sorted:  ', last_sorted)
    print('time:', t1)
    print()
    assert last_sorted == asc_list
    times.append(t1)

    if len(unsorted) > 1:
        print_list('asc_list:', asc_list)
        t2 = timeit(lambda: sort_copy(asc_list), number = repeat)
        print_list('sorted:  ', last_sorted)
        print('time:', t2)
        print()
        assert last_sorted == asc_list
        times.append(t2)

        desc_list = sorted(unsorted, reverse = True)
        print_list('dsc_list:', desc_list)
        t3 = timeit(lambda: sort_copy(desc_list), number = repeat)
        print_list('sorted:  ', last_sorted)
        print('time:', t3)
        print()
        assert last_sorted == asc_list
        times.append(t3)

    print('-'*33, sort_func.__name__, 'end', '-'*33)
    print()

    return times

def counting_sort(arr):
    """
    计数排序,将元素值作为数组下标

    :bucket_len:
    :max_value: 需要指明待排数组的最大元素值
    """
    bucket_len = max(arr) + 1
    bucket = [0] * bucket_len
    sorted_index = 0
    arr_len = len(arr)
    for i in range(arr_len):
        if not bucket[arr[i]]:
            bucket[arr[i]] = 0
        bucket[arr[i]] += 1
    for j in range(bucket_len):
        while bucket[j] > 0:
            arr[sorted_index] = j
            sorted_index += 1
            bucket[j] -= 1
    return arr


if __name__ == '__main__':
    li = randint_list()
    max_value = max(li)
    print(li, f'max_value: {max_value}')
    print(counting_sort(li))
def binaryInsert(arr):
    # 折半插入排序: 小->大
    # 在直接插入排序的基础上使用了折半查找的方法
    for i in range(1, len(arr)):
        index = arr[i]
        low = 0
        high = i - 1
        while low <= high:
            mid = low + ((high - low) >> 1)
            if index > arr[mid]:
                low = mid + 1
            else:
                high = mid - 1
        # 跳出循环后 low, mid 都是一样的,其实不一样 hight = low - 1
        for j in range(i, low, -1):
            # print(j)
            arr[j] = arr[j - 1]
        arr[low] = index
    print(arr)
    return arr


if __name__ == '__main__':
    li = randint_list(start=0, stop=1e6, length=10000)
    # li = [6, 3, 1, 2, 5, 7, 4]
    # print(li)
    # insert_sort(li)
    # bin_insert_sort(li)
    insertionSort(li)
    # binary_search(li, 5)
Ejemplo n.º 4
0
        quick_sort(arr, pivot + 1, right)
    return arr


def partition(arr, left, right):
    pivot = arr[left]  # 第一个元素作为枢纽
    while left < right:  # left == right 时跳出
        while left < right and arr[right] >= pivot:
            # 从右往左找到第一个比 pivot 小的元素的下标
            right -= 1
        arr[left] = arr[right]  # 将其放到 pivot 左边
        while left < right and arr[left] <= pivot:
            # 从左往右找到第一个比 pivot 大的元素的下标
            left += 1
        arr[right] = arr[left]  # 将其放到 pivot 右边
    arr[left] = pivot  # 此时left、right两个指针指向相同位置
    return left


@timer
def main(arr):
    quick_sort(arr)


if __name__ == '__main__':
    li = randint_list(0, 1e6, 10000)
    print(li)
    quick_sort(li)
    # lii = quick_sort(li)
    # print(lii)
Ejemplo n.º 5
0
import sys
from randint_list import randint_list
from timeit import timeit
from sort_test import sort_test
from buble_sort import buble_sort
from insertion_sort import insertion_sort
from shell_sort import shell_sort
from selection_sort import selection_sort
from merge_sort import merge_sort
from quick_sort import quick_sort
from quick_sort import quick_sort2

sys.setrecursionlimit(10**5)
nos = randint_list(10000, no_ext=500000)


def test(sort_func):
    return sort_test(sort_func, nos=nos, repeat=1)


def show_times(tsmap, msg):
    print(msg)

    for x in sorted(tsmap.items(), key=lambda x: x[1]):
        print('\t', x)

    print()


if __name__ == '__main__':
    sort_funcs = [