六大排序算法及部分动画演示(Python)

import matplotlib.pyplot as plt
import numpy as np
from IPython.display import display, clear_output
from palettable.colorbrewer.qualitative import *  # 调色板

六大排序算法及部分动画演示(Python)_第1张图片

冒泡排序

def bubble_sort(arr):
    list_len = len(arr)
    try:
        arr = arr.tolist()
    except:
        pass
    x_ticks = list(range(list_len))
    
    '''图'''
    fig, ax = plt.subplots()
    ax.bar(x_ticks, height=arr, color=Set2_8.hex_colors)
    plt.pause(0.5)

    for i in range(list_len-1):
        flag = False
        for j in range(list_len - i - 1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
                flag = True
            
            """图"""
            ax.cla()
            ax.bar(x_ticks, height=arr, color=Set2_8.hex_colors)
            clear_output(wait = True)
            display(fig)
            clear_output(wait = True)
            plt.pause(0.01)
            
        if flag == False:
            break
            
    display(fig)
    
    return arr

选择排序

def select_sort(arr):
    list_len = len(arr)
    try:
        arr = arr.tolist()
    except:
        pass
    x_ticks = list(range(list_len))
    
    '''图'''
    fig, ax = plt.subplots()
    ax.bar(x_ticks, height=arr, color=Set2_8.hex_colors)
    plt.pause(0.5)
    
    for i in range(list_len):
        
        min_index = i
        for j in range(i+1, list_len):
            if arr[j] < arr[min_index]:
                min_index = j
        
        if min_index != i:
            arr[i], arr[min_index] = arr[min_index], arr[i]

        """图"""
        ax.cla()
        ax.bar(x_ticks, height=arr, color=Set2_8.hex_colors)
        clear_output(wait = True)
        display(fig)
        clear_output(wait = True)
        plt.pause(0.01)

    display(fig)

    return arr

插入排序

def insert_sort(arr):
    list_len = len(arr)
    try:
        arr = arr.tolist()
    except:
        pass
    x_ticks = list(range(list_len))
    
    '''图'''
    fig, ax = plt.subplots()
    ax.bar(x_ticks, height=arr, color=Set2_8.hex_colors)
    plt.pause(0.5)
    
    for i in range(list_len-1):
        for j in range(i+1,0,-1):
            if arr[j] < arr[j-1]:
                arr[j], arr[j-1] = arr[j-1], arr[j]
                
                """图"""
                ax.cla()
                ax.bar(x_ticks, height=arr, color=Set2_8.hex_colors)
                clear_output(wait = True)
                display(fig)
                clear_output(wait = True)
                plt.pause(0.01)
            else:
                break
                
    return arr

希尔排序

def shell_sort(arr):
    list_len = len(arr)
    try:
        arr = arr.tolist()
    except:
        pass
    x_ticks = list(range(list_len))
    
    '''图'''
    fig, ax = plt.subplots()
    ax.bar(x_ticks, height=arr, color=Set2_8.hex_colors)
    plt.pause(0.5)
    
    temp = list_len // 2
    while temp > 0:
        for k in range(temp):        
            for i in range(k+temp,list_len,temp):
                for j in range(i,k,-temp):
                    if arr[j] < arr[j-temp]:
                        arr[j], arr[j-temp] = arr[j-temp], arr[j]
                        
                        """图"""
                        ax.cla()
                        ax.bar(x_ticks, height=arr, color=Set2_8.hex_colors)
                        clear_output(wait = True)
                        display(fig)
                        clear_output(wait = True)
                        plt.pause(0.01)
                    else:
                        break
                        
                    
        temp //= 2

快速排序

def swap(lst,i,j):
    lst[i], lst[j] = lst[j], lst[i]
    
def median3(lst,left,right):
    mid = (left + right) // 2
    if lst[left] > lst[mid]:
        swap(lst, left, mid)
    if lst[left] > lst[right]:
        swap(lst, left, right)
    if lst[mid] > lst[right]:
        swap(lst, mid, right)
    swap(lst, mid, right-1)
    
    return lst[right-1]

def quicksort(arr, left, right):
    list_len = right - left + 1
    if list_len < 2:
        return
    
    pivot = median3(arr, left, right)
    i = left
    j = right - 2
    while True:
        while arr[i] < pivot:
            i+=1
        while arr[j] > pivot:
            j-=1
        if i < j:
            swap(arr, i, j)
        else:
            swap(arr, i, right-1)
            break
            
    quicksort(arr, left, i-1)
    quicksort(arr, i+1, right)
    
    return arr

def quick_sort(arr):
    try:
        arr = arr.tolist()
    except:
        pass
    left = 0
    right = len(arr) - 1
    quicksort(arr, left, right)
    
    return arr

归并排序

def merge(l_lst, r_lst):
    temp = []
    while l_lst and r_lst:
        if l_lst[0] <= r_lst[0]:
            temp.append(l_lst.pop(0))
        else:
            temp.append(r_lst.pop(0))
    while l_lst:
        temp.append(l_lst.pop(0))
    while r_lst:
        temp.append(r_lst.pop(0))
        
    return temp

def merge_sort(arr):
    try:
        arr = arr.tolist()
    except:
        pass
    list_len = len(arr)
    if list_len < 2:
        return arr
    mid = list_len // 2
    left = merge_sort(arr[0:mid])
    right = merge_sort(arr[mid:])
    return merge(left, right)

你可能感兴趣的:(Python绘图,排序算法,python)