wiki搬运,排序算法

稳定的排序[编辑]

  • 冒泡排序(bubble sort)— {\displaystyle O(n^{2})}O(n^{2})
  • 插入排序(insertion sort)—{\displaystyle O(n^{2})}O(n^{2})
  • 鸡尾酒排序(cocktail sort)—{\displaystyle O(n^{2})}O(n^{2})
  • 桶排序(bucket sort)—{\displaystyle O(n)}O(n);须要{\displaystyle O(k)}O(k)额外空间
  • 计数排序(counting sort)—{\displaystyle O(n+k)}O(n+k);须要{\displaystyle O(n+k)}O(n+k)额外空间
  • 归并排序(merge sort)—{\displaystyle O(n\log n)}O(n\log n);须要{\displaystyle O(n)}O(n)额外空间
  • 原地归并排序— {\displaystyle O(n\log ^{2}n)}O(n\log^2 n)若是使用最佳的如今版本
  • 二叉排序树排序(binary tree sort)— {\displaystyle O(n\log n)}O(n\log n)指望时间;{\displaystyle O(n^{2})}O(n^{2})最坏时间;须要{\displaystyle O(n)}O(n)额外空间
  • 鸽巢排序(pigeonhole sort)—{\displaystyle O(n+k)}O(n+k);须要{\displaystyle O(k)}O(k)额外空间
  • 基数排序(radix sort)—{\displaystyle O(nk)}{\displaystyle O(nk)};须要{\displaystyle O(n)}O(n)额外空间
  • 侏儒排序(gnome sort)— {\displaystyle O(n^{2})}O(n^{2})
  • 图书馆排序(library sort)— {\displaystyle O(n\log n)}O(n\log n)指望时间;{\displaystyle O(n^{2})}O(n^{2})最坏时间;须要{\displaystyle (1+\varepsilon )n}{\displaystyle (1+\varepsilon )n}额外空间
  • 块排序(block sort)— {\displaystyle O(n\log n)}O(n\log n)

不稳定的排序[编辑]

  • 选择排序(selection sort)—{\displaystyle O(n^{2})}O(n^{2})
  • 希尔排序(shell sort)—{\displaystyle O(n\log ^{2}n)}O(n\log^2 n)若是使用最佳的如今版本
  • 克洛弗排序(Clover sort)—{\displaystyle O(n)}O(n)指望时间,{\displaystyle O(n^{2})}O(n^{2})最坏状况
  • 梳排序— {\displaystyle O(n\log n)}O(n\log n)
  • 堆排序(heap sort)—{\displaystyle O(n\log n)}O(n\log n)
  • 平滑排序(smooth sort)— {\displaystyle O(n\log n)}O(n\log n)
  • 快速排序(quick sort)—{\displaystyle O(n\log n)}O(n\log n)指望时间,{\displaystyle O(n^{2})}O(n^{2})最坏状况;对于大的、随机数列表通常相信是最快的已知排序
  • 内省排序(introsort)—{\displaystyle O(n\log n)}O(n\log n)
  • 耐心排序(patience sort)—{\displaystyle O(n\log n+k)}{\displaystyle O(n\log n+k)}最坏状况时间,须要额外的{\displaystyle O(n+k)}O(n+k)空间,也须要找到最长的递增子序列(longest increasing subsequence)

不实用的排序[编辑]

  • Bogo排序— {\displaystyle O(n\times n!)}{\displaystyle O(n\times n!)},最坏的状况下指望时间为无穷。
  • Stupid排序{\displaystyle O(n^{3})}O(n^{3});递归版本须要{\displaystyle O(n^{2})}O(n^{2})额外存储器
  • 珠排序(bead sort)— {\displaystyle O(n)}O(n) 或 {\displaystyle O({\sqrt {n}})}O({\sqrt  {n}}),但须要特别的硬件
  • 煎饼排序{\displaystyle O(n)}O(n),但须要特别的硬件
  • 臭皮匠排序(stooge sort)算法简单,但须要约{\displaystyle n^{2.7}}{\displaystyle n^{2.7}}的时间

 

名称 数据对象 稳定性 时间复杂度 额外空间复杂度 描述
平均 最坏
冒泡排序 数组 {\displaystyle O(n^{2})}O(n^{2}) {\displaystyle O(1)}O(1) (无序区,有序区)。
从无序区透过交换找出最大元素放到有序区前端。
选择排序 数组 {\displaystyle O(n^{2})}O(n^{2}) {\displaystyle O(1)}O(1) (有序区,无序区)。
在无序区里找一个最小的元素跟在有序区的后面。对数组:比较得多,换得少。
链表
插入排序 数组、链表 {\displaystyle O(n^{2})}O(n^{2}) {\displaystyle O(1)}O(1) (有序区,无序区)。
把无序区的第一个元素插入到有序区的合适的位置。对数组:比较得少,换得多。
堆排序 数组 {\displaystyle O(n\log n)}O(n\log n) {\displaystyle O(1)}O(1) (最大堆,有序区)。
从堆顶把根卸出来放在有序区以前,再恢复堆。
归并排序 数组 {\displaystyle O(n\log ^{2}n)}{\displaystyle O(n\log ^{2}n)} {\displaystyle O(1)}{\displaystyle O(1)} 把数据分为两段,从两段中逐个选最小的元素移入新数据段的末尾。
可从上到下或从下到上进行。
{\displaystyle O(n\log n)}O(n\log n) {\displaystyle O(n)+O(\log n)}O(n)+O(\log n)
若是不是从下到上
链表 {\displaystyle O(1)}O(1)
快速排序 数组 {\displaystyle O(n\log n)}O(n\log n) {\displaystyle O(n^{2})}O(n^{2}) {\displaystyle O(\log n)}O(\log n) (小数,基准元素,大数)。 
在区间中随机挑选一个元素做基准,将小于基准的元素放在基准以前,大于基准的元素放在基准以后,再分别对小数区与大数区进行排序。
希尔排序 数组 {\displaystyle O(n\log ^{2}n)}O(n\log ^{2}n) {\displaystyle O(n^{2})}O(n^{2}) {\displaystyle O(1)}O(1) 每一轮按照事先决定的间隔进行插入排序,间隔会依次缩小,最后一次必定要是1。
 
计数排序 数组、链表 {\displaystyle O(n+m)}O(n+m) {\displaystyle O(n+m)}O(n+m) 统计小于等于该元素值的元素的个数i,因而该元素就放在目标数组的索引i位(i≥0)。
桶排序 数组、链表 {\displaystyle O(n)}O(n) {\displaystyle O(m)}O(m) 将值为i的元素放入i号桶,最后依次把桶里的元素倒出来。
基数排序 数组、链表 {\displaystyle O(k\times n)}O(k\times n) {\displaystyle O(n^{2})}O(n^{2})   一种多关键字的排序算法,可用桶排序实现。
    • 均按从小到大排列
    • k表明数值中的"数字"个数
    • n表明数据规模
    • m表明数据的最大值减最小值
相关文章
相关标签/搜索