排序算法

1、冒泡排序

def bubble_sort(alist):
    """冒泡排序"""
    n = len(alist)
    for j in range(0,n-1):
        count = 0
        for i in range(0,n-1-j):
            # 从头走到尾
            if alist[i] > alist[i+1]:
                count += 1
                alist[i],alist[i+1] = alist[i+1],alist[i]
            print(j,i)
        if 0 == count:
            break
    return alist

if __name__ == '__main__':
    a = [1,5,3,6,823,242,6,2,4,5,6]
    ret = bubble_sort(a)
    print(ret)

    li = [54, 26, 93, 17, 77, 31, 44, 55, 20]
    bubble_sort(li)
    print(li)

    li = [1,2,3,4,5]
    bubble_sort(li)
    print(li)

2、选择排序

def select_sort(alist):
    """选择排序"""
    n = len(alist)
    for j in range(0,n-1):
        min_index = j
        for i in range(j+1,n):
            # 从j+1走到尾
            if alist[i] < alist[min_index]:
                min_index = i
        alist[j],alist[min_index] = alist[min_index],alist[j]
        print(alist)


if __name__ == '__main__':
    a = [1,5,3,6,823,242,6,2,4,5,6]
    select_sort(a)
    # print(a)

    li = [54, 26, 93, 17, 77, 31, 44, 55, 20]
    select_sort(li)
    print(li)

3、插入排序

def insert_sort(alist):
    """插入排序"""
    n = len(alist)

    # 从右边的无序序列中取出多少个元素执行这样的过程
    for j in range(1,n):
        # j = [1,2,3,...n-1]
        # i 代表内层循环起始
        i = j
        # 执行从右边的无序序列中取出第一个元素,即i位置的元素,然后将其插入前面的正确位置中
        while i> 0:
            if alist[i] < alist[i-1]:
                alist[i-1],alist[i] = alist[i],alist[i-1]
                i -= 1
            else:
                break
        print(alist)


if __name__ == '__main__':
    a = [1,5,3,6,823,242,6,2,4,5,6]
    insert_sort(a)
    # print(a)

    li = [54, 26, 93, 17, 77, 31, 44, 55, 20]
    insert_sort(li)
    print(li)

 

4、希尔排序

def shell_sort(alist):
    """希尔排序"""
    n = len(alist)
    # n =9
    gap = n//2

    # gap变化到0之前,插入算法执行的次数
    while gap >=1:
        # 插入算法, 与普通的插入算法的区别就是gap步长
        for j in range(gap,n):
            # j = [gap, gap+1, gap+2, ..., n-1]
            i = j
            while i >0:
                if alist[i] < alist[i-gap]:
                    alist[i], alist[i-gap] = alist[i-gap], alist[i]
                    i -= gap
                else:
                    break
        # 缩短gap步长
        gap //= 2


if __name__ == '__main__':
    a = [1,5,3,6,823,242,6,2,4,5,6]
    shell_sort(a)
    print(a)

    li = [54, 26, 93, 17, 77, 31, 44, 55, 20]
    shell_sort(li)
    print(li)

5、快速排序

def quick_sort(alist, first, last):
    """快速排序"""
    if first >= last:
        return

    mid_value = alist[first]
    low = first
    high = last

    while low < high:
        # high游标左移
        while low < high and alist[high] >= mid_value:
            high -= 1
        alist[low] = alist[high]

        # low游标右移
        while low < high and alist[low] < mid_value:
            low += 1
        alist[high] = alist[low]

    # 从循环退出时,low==high
    alist[low] = mid_value

    # 对low左边的列表执行快速排序
    quick_sort(alist, first, low-1)

    # 对low右边的列表执行快速排序
    quick_sort(alist, low+1, last)

if __name__ == '__main__':
    a = [1,5,3,6,823,242,6,2,4,5,6]
    quick_sort(a,0,len(a)-1)
    print(a)

    li = [54, 26, 93, 17, 77, 31, 44, 55, 20]
    quick_sort(li,0,len(li)-1)
    print(li)

 

6、归并排序

def merge_sort(alist):
    """归并排序"""
    n = len(alist)
    if n <= 1:
        return alist
    mid = n//2
    # left_li 采用归并排序后形成的有序的新的列表
    left_li =merge_sort(alist[:mid])
    print("left_li :", left_li)
    # right_li 采用归并排序后形成的有序的新的列表
    right_li = merge_sort(alist[mid:])

    print("right_li:",right_li)

    #将两个有序的子序列合并为一个新的整体
    # merge(left,right)
    left_pointer , right_pointer = 0,0
    result = []

    while left_pointer < len(left_li) and right_pointer < len(right_li):
        if left_li[left_pointer] < right_li[right_pointer]:
            result.append(left_li[left_pointer])
            left_pointer += 1
        else:
            result.append(right_li[right_pointer])
            right_pointer += 1

    result += left_li[left_pointer:]
    result += right_li[right_pointer:]

    print("result :", result)
    print("*"*50)
    return result

if __name__ == '__main__':
    a = [1,5,3,6,823,242,6,2,4,5,6]
    ret = merge_sort(a)
    print(ret)

    li = [54, 26, 93, 17, 77, 31, 44, 55, 20]
    ret = merge_sort(li)
    print(ret)

 

7、侏儒排序

def gnomesort(seq):
    '''侏儒排序'''
    i = 0
    while i < len(seq):
        if i == 0 or seq[i-1] <= seq[i]:
            print("+:",i)
            i += 1
        else:
            print("修改:",i)
            seq[i], seq[i-1] = seq[i-1], seq[i]
            print(a)
            i -= 1

if __name__ == '__main__':
    a = [1,5,3,6,823,242,6,2,4,5,6]
    gnomesort(a)
    print(a)

    li = [54, 26, 93, 17, 77, 31, 44, 55, 20]
    gnomesort(li)
    print(li)

    li = [1,2,3,4,5]
    gnomesort(li)
    print(li)

 

posted @ 2018-12-08 23:38  RobotsRising  阅读(154)  评论(0编辑  收藏  举报