无聊python小程序zhi 排序+动图

仅供娱乐
参考内容1: 百度百科
参考内容2: 菜鸟教程

  • 冒泡排序
def  bubble_sort(A):
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    A = np.array(A)
    plt.ion()
    x = np.arange(0,len(A))
    while True :
        number = 0
        for i in range(len(A)-1):
            if A[i] > A[i+1]:
                A[i],A[i+1] = A[i+1], A[i]
                number += 1
        ax.cla()
        ax.bar(x, A)
        plt.pause(0.1)
        if number == 0:
            break
    print(A)
    plt.ioff()
    plt.show()
  • 选择排序
def  selection_sort(A):
    plt.clf()
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    plt.ion()
    A = np.array(A)
    x = np.arange(len(A))
    for i in range((len(A))):
        index_min = i
        for j in range(i,len(A)):
            if A[j] < A[index_min]:
                index_min = j
        A[i],A[index_min] = A[index_min], A[i]
        ax.cla()
        ax.bar(x,A)
        plt.pause(0.01)
    print(A)
    plt.ioff()
    plt.show()
  • 插入排序
def insert_sort(A):
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    plt.ion()
    L = [max(A)+1]
    A = np.array(A)
    ii = 0
    for i in A :
        ii += 1
        for j in range(len(L)):
            if i < L[j]:
                L.insert(j,i)
                break
        ax.cla()
        x = np.arange(len(L)-1)
        ax.bar(x,L[:-1])
        plt.pause(0.01)
    L = L[:-1]
    plt.ioff()
    plt.show()
  • 希尔排序
def Shell_sort(A):
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    plt.ion()
    length = int(len(A)/2)
    while True:
        for i in range(length,len(A)):
            number = A[i]
            g = i - length
            while A[g] > number and g >= 0 :
                A[g + length] = A[g]
                g -= length
            A[g+length] = number
        ax.cla()
        ax.bar(np.arange(len(A)),A)
        plt.pause(0.3)
        length = int(length/2)
        if length < 1 :
            break
    print(A)
    plt.ioff()
    plt.show()
  • 快速排序
def quick_sort(A):
    if len(A) >= 2 :
        left, right = [],[]
        temp = A[0]
        A.remove(temp)
        for item in A :
            if item >= temp:
                right.append(item)
            else:
                left.append(item)
        return quick_sort(left) + [temp] + quick_sort(right)
    else:
        return A
  • 归并排序
class Merge_sort(object):
    def __init__(self,A):
        self.A = list(A)
        print(self.merge(self.A))

    def merge(self,lis):
        if len(lis) <= 1:
            return lis
        cut = len(lis)//2
        left = self.merge(lis[:cut])
        right = self.merge(lis[cut:])
        return self.sort(left,right)

    def sort(self,left,right):
        rel,rig = 0,0
        result = []
        while rel < len(left) and rig < len(right):
            if left[rel] < right[rig]:
                result.append(left[rel])
                rel += 1
            else:
                result.append(right[rig])
                rig += 1
        result += list(left[rel:])
        result += list(right[rig:])
        return result
  • 堆排序
class Heap_Sort(object):

    def __init__(self,array):
        self.A = array
        self.Heap_sort()

    def SubSort(self,num, index):
        larg = index
        l = index*2 + 1
        r = index*2 + 2
        if l < num and self.A[index] < self.A[l] :
            larg = l
        if r < num and self.A[larg] < self.A[r] :
            larg = r
        if larg != index:
            self.A[index],self.A[larg] = self.A[larg],self.A[index]
            self.SubSort(num, larg)

    def Heap_sort(self):
        plt.ion()
        fig = plt.figure()
        ax = fig.add_subplot(1,1,1)

        num = len(self.A)
        for i in range(num, -1, -1):
            self.SubSort(num, i)

        for i in range(num-1, 0, -1):
            self.A[i],self.A[0] = self.A[0],self.A[i]
            self.SubSort(i,0)
            ax.cla()
            ax.bar(np.arange(len(self.A)),self.A)
            plt.pause(0.01)
        plt.ioff()
        plt.show()
        print(self.A)
  • 基数排序
def Radix_sort(A):
    plt.ion()
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    A = list(A)
    length = 0
    lar = max(A)
    while lar != 0 :
        length += 1
        lar //= 10
    for i in range(1,4):
        per = 1
        for ii in range(i-1):
            per *= 10
        bucket = [[] for ii in range(10)]
        print(per)
        # print(bucket)
        for item in A :
            if i == 1:
                num = item % 10
                bucket[num].append(item)
            else:
                num = (item // per) % 10
                bucket[num].append(item)
        A = []
        for j in range(len(bucket)):
            for jj in bucket[j]:
                A.append(jj)
        ax.cla()
        ax.bar(np.arange(len(A)),A)
        plt.pause(0.3)
    plt.ioff()
    plt.show()
    # print(A)
  • 测试
A = np.random.randint(0,1000,100)
# bubble_sort(A)
# selection_sort(A)
# insert_sort(A)
# Shell_sort(A)
# quick_sort(list(A))
# Merge_sort(A)
# Heap_Sort(A)
# Radix_sort(A)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值