Weaving Wong

总觉得该写点啥...

嗨,我是Weaving,一名机器学习爱好者.


分享读书、学习、生活感悟

七种排序方法

冒泡排序(稳定)—O(n²)

  • 基本思想:两两比较相邻记录的关键字,如果反序则交换,直到没有反序的记录为止。
def bubble_sort(alist):
    n = len(alist)
    for j in range(n - 1):
        count = 0
        for i in range(0, n - 1 - j):
            if alist[i] > alist[i + 1]:
                alist[i], alist[i + 1] = alist[i + 1], alist[i]
                count += 1
        print(alist)
        if (count == 0):
            break


if __name__ == '__main__':
    alist = [5, 4, 3, 6, 2, 9, 1, 8, 7, 0]
    print(alist)
    bubble_sort(alist)
[5, 4, 3, 6, 2, 9, 1, 8, 7, 0]
[4, 3, 5, 2, 6, 1, 8, 7, 0, 9]
[3, 4, 2, 5, 1, 6, 7, 0, 8, 9]
[3, 2, 4, 1, 5, 6, 0, 7, 8, 9]
[2, 3, 1, 4, 5, 0, 6, 7, 8, 9]
[2, 1, 3, 4, 0, 5, 6, 7, 8, 9]
[1, 2, 3, 0, 4, 5, 6, 7, 8, 9]
[1, 2, 0, 3, 4, 5, 6, 7, 8, 9]
[1, 0, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

选择排序(不稳定)—O(n²)

  • 首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。
def sellect_sort(alist):
    n = len(alist)
    for i in range(n):
        min = i
        for j in range(i + 1, n):
            if alist[min] > alist[j]:
                min = j
        alist[min], alist[i] = alist[i], alist[min]
        print(alist)
    return alist


if __name__ == '__main__':
    alist = [5, 4, 3, 6, 2, 9, 1, 8, 7, 0]
    print(alist)
    sellect_sort(alist)
[5, 4, 3, 6, 2, 9, 1, 8, 7, 0]
[0, 4, 3, 6, 2, 9, 1, 8, 7, 5]
[0, 1, 3, 6, 2, 9, 4, 8, 7, 5]
[0, 1, 2, 6, 3, 9, 4, 8, 7, 5]
[0, 1, 2, 3, 6, 9, 4, 8, 7, 5]
[0, 1, 2, 3, 4, 9, 6, 8, 7, 5]
[0, 1, 2, 3, 4, 5, 6, 8, 7, 9]
[0, 1, 2, 3, 4, 5, 6, 8, 7, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

插入排序(稳定)—O(n**2)

  • 通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间。
def inser_sort(alist):
    n = len(alist)
    for i in range(1, n):
        # 控制将拿到的元素放到前面的有序序列中的正确位置的过程
        for j in range(i, 0, -1):
            # 如果比前面的元素小,则往前移
            if alist[j] < alist[j - 1]:
                alist[j], alist[j - 1] = alist[j - 1], alist[j]
                print(alist)
            else:
                break


if __name__ == '__main__':
    alist = [5, 4, 3, 6, 2, 9, 1, 8, 7, 0]
    print(alist)
    inser_sort(alist)
[5, 4, 3, 6, 2, 9, 1, 8, 7, 0]
[4, 5, 3, 6, 2, 9, 1, 8, 7, 0]
[4, 3, 5, 6, 2, 9, 1, 8, 7, 0]
[3, 4, 5, 6, 2, 9, 1, 8, 7, 0]
[3, 4, 5, 2, 6, 9, 1, 8, 7, 0]
[3, 4, 2, 5, 6, 9, 1, 8, 7, 0]
[3, 2, 4, 5, 6, 9, 1, 8, 7, 0]
[2, 3, 4, 5, 6, 9, 1, 8, 7, 0]
[2, 3, 4, 5, 6, 1, 9, 8, 7, 0]
[2, 3, 4, 5, 1, 6, 9, 8, 7, 0]
[2, 3, 4, 1, 5, 6, 9, 8, 7, 0]
[2, 3, 1, 4, 5, 6, 9, 8, 7, 0]
[2, 1, 3, 4, 5, 6, 9, 8, 7, 0]
[1, 2, 3, 4, 5, 6, 9, 8, 7, 0]
[1, 2, 3, 4, 5, 6, 8, 9, 7, 0]
[1, 2, 3, 4, 5, 6, 8, 7, 9, 0]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
[1, 2, 3, 4, 5, 6, 7, 8, 0, 9]
[1, 2, 3, 4, 5, 6, 7, 0, 8, 9]
[1, 2, 3, 4, 5, 6, 0, 7, 8, 9]
[1, 2, 3, 4, 5, 0, 6, 7, 8, 9]
[1, 2, 3, 4, 0, 5, 6, 7, 8, 9]
[1, 2, 3, 0, 4, 5, 6, 7, 8, 9]
[1, 2, 0, 3, 4, 5, 6, 7, 8, 9]
[1, 0, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

希尔排序(不稳定)—O(n^(3/2))

  • 也称递减增量排序算法,是插入排序的一种更高效的改进版本。希尔排序是非稳定排序算法。
def shell_sort(alist):
    n = len(alist)
    gap = n // 2
    while gap >= 1:
        for j in range(gap, n):
            i = j
            while (i - gap) >= 0:
                print(i,gap)
                if alist[i] < alist[i - gap]:
                    alist[i], alist[i - gap] = alist[i - gap], alist[i]
                    print(alist)
                    i -= gap
                else:
                    break
        gap //= 2
if __name__ == '__main__':
    alist = [5, 4, 3, 6, 2, 9, 1, 8, 7, 0]
    print(alist)
    shell_sort(alist)
[5, 4, 3, 6, 2, 9, 1, 8, 7, 0]
5 5
6 5
[5, 1, 3, 6, 2, 9, 4, 8, 7, 0]
7 5
8 5
9 5
[5, 1, 3, 6, 0, 9, 4, 8, 7, 2]
2 2
[3, 1, 5, 6, 0, 9, 4, 8, 7, 2]
3 2
4 2
[3, 1, 0, 6, 5, 9, 4, 8, 7, 2]
2 2
[0, 1, 3, 6, 5, 9, 4, 8, 7, 2]
5 2
6 2
[0, 1, 3, 6, 4, 9, 5, 8, 7, 2]
4 2
7 2
[0, 1, 3, 6, 4, 8, 5, 9, 7, 2]
5 2
8 2
9 2
[0, 1, 3, 6, 4, 8, 5, 2, 7, 9]
7 2
[0, 1, 3, 6, 4, 2, 5, 8, 7, 9]
5 2
[0, 1, 3, 2, 4, 6, 5, 8, 7, 9]
3 2
1 1
2 1
3 1
[0, 1, 2, 3, 4, 6, 5, 8, 7, 9]
2 1
4 1
5 1
6 1
[0, 1, 2, 3, 4, 5, 6, 8, 7, 9]
5 1
7 1
8 1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
7 1
9 1







最近的文章

牛顿法与二分法求平方根

牛顿法求平方根# __*__ coding:utf8 __*__import mathfrom math import sqrt# 利用牛顿法求平方根def sqrt_newton(a): if a < 1e-6: return 0 x , y = (a,0) count = 0 while abs(y-x) > 0.00000001: count += 1 y = x print(count,'\...…

继续阅读
更早的文章

华为算法精英大赛(DigiX)“用户人口属性预测”Rank14方案开源与总结

比赛地址:华为算法精英大赛“用户人口属性预测” 方案开源地址:WeavingWong的Github1. 赛题理解(1) 待解决的问题对于手机设备厂商,获取当前手机用户的人口属性信息(demographics)非常困难,当前华为手机3.5亿用户中,大概只有5000万用户的性别和年龄信息,如何基于用户的手机及使用偏好准确地预测其人口属性信息是提升个性化体验、构建精准用户画像的基础。手机用户的人口属性(如性别、年龄、常驻地等)数据一方面可以被用于个性化推荐服务,提升用户体验,另一方面可以用...…

年龄段预测继续阅读