[Python] 【基础排序算法集合】Pyqt可视化

时间复杂度高:冒泡排序、选择排序。
时间复杂度低:基数排序、堆排序、希尔排序、归并排序、快速排序。

程序特点:
1、pyqt可视化;
2、开通多线程运行,可以同时对多个排序算法进行运行时间比较
3、可一次性生成一万个随机数,运用于测试

class SelectSort():
    def __init__(self, arr):
        self.start(arr)
    def swap(self, arr, a, b):
        temp = arr[a]
        arr[a] = arr[b]
        arr[b] = temp
    def start(self, arr):
        length = len(arr)
        for i in range(length):
            the_min = i
            for j in range(i + 1, length):
                if arr[j] < arr[the_min]:
                    the_min = j
            self.swap(arr, the_min, i)
class QuickSort():
    def __init__(self, arr):
        self.start(arr, 0, len(arr) - 1)
    def swap(self, arr, a, b):
        temp = arr[a]
        arr[a] = arr[b]
        arr[b] = temp
    def start(self, array, low, high):
        if low > high:
            return
        pivot = low
        left_p = low
        right_p = high
        while left_p < right_p:
            while array[right_p] >= array[pivot] and left_p < right_p:
                right_p -= 1
            while array[left_p] <= array[pivot] and left_p < right_p:
                left_p += 1
            if left_p < right_p:
                self.swap(array, right_p, left_p)
        self.swap(array, pivot, left_p)
        self.start(array, low, left_p - 1)
        self.start(array, left_p + 1, high)
class ShellSort():
    def __init__(self, arr):
        self.start(arr)
    def start(self, arr):
        n = len(arr)
        gap = int(n / 2)
        while gap > 0:
            for i in range(gap, n):
                temp = arr[i]
                j = i - gap
                while j >= 0 and arr[j] > temp:
                    arr[j + gap] = arr[j]
                    j = j - gap
                arr[j + gap] = temp
            gap = int(gap / 2)
class HeapSort():
    def __init__(self, arr):
        self.start(arr)
    def swap(self, arr, a, b):
        temp = arr[a]
        arr[a] = arr[b]
        arr[b] = temp
    def Percdown(self, arr, now, length):
        replace = now * 2
        while replace < length:
            right_child = replace + 1
            if right_child < length and arr[replace] < arr[right_child]:
                replace = right_child
            if arr[replace] > arr[now]:
                self.swap(arr, replace, now)
                now = replace
                replace = now * 2
            else:
                break
    def start(self, array):
        length = len(array)
        for i in range(length // 2, -1, -1):
            self.Percdown(array, i, length)
        for i in range(length - 1, -1, -1):
            self.swap(array, 0, i)
            self.Percdown(array, 0, i)
class RadixSort():
    def __init__(self, arrx):
        self.start(arrx)
    def start(self, arr):
        # buckets有十个桶
        buckets = []
        for i in range(10):
            buckets.append([])
        # 基数设置为6位
        for i in range(6):
            for j in arr:
                buckets[(j // int(pow(10, i))) % 10].append(j)
            # 清空 arr
            arr.clear()
            # 重新排序 并放入arr中
            for k in buckets:
                for z in k:
                    arr.append(z)
            # 清空所有桶内容
            for u in buckets:
                u.clear()
class MergeSort():
    def __init__(self, arr):
        self.start(arr,0,len(arr)-1)
    def start(self, array,low,high):
        if low

运行结果
THE END
分享
二维码
< <上一篇
下一篇>>
文章目录
关闭
目 录