Graphical explanation of 8 major sorting algorithms

Sorting algorithms can be divided into internal sorting and external sorting. Internal sorting is the sorting of data records in memory, while external sorting is because the data to be sorted is large and cannot accommodate all sorting records at one time, and external memory needs to be accessed during the sorting process.

Common internal sorting algorithms are: Insertion Sort, Hill Sort, Selection Sort, Bubble Sort, Merge Sort, Quick Sort, Heap Sort, Radix Sort, etc.

This experience will explain to you the graphical steps of the 8 major sorting algorithms.

method/step

  1. Insertion sort

    Insertion sort is one of the simplest and most intuitive sorting algorithms. Its working principle is to construct an ordered sequence. For unsorted data, scan from back to front in the sorted sequence, find the corresponding position and insert it.

    Algorithm steps:

    1) Treat the first element of the first to-be-sorted sequence as an ordered sequence, and treat the second to last element as an unsorted sequence.

    2) Scan the unsorted sequence sequentially from the beginning to the end, and insert each element scanned into the appropriate position of the sorted sequence. (If the element to be inserted is equal to an element in the ordered sequence, the element to be inserted is inserted after the equal element.)

    Graphical explanation of 8 major sorting algorithms



  2. Hill sort
    Hill sort, also known as the decreasing-increment sorting algorithm, is a more efficient and improved version of insertion sort. But Hill sort is a non-stable sorting algorithm.
    Hill sort is an improved method based on the following two properties of insertion sort:
    Insertion sort has high efficiency when operating on almost sorted data, that is, the efficiency of linear sorting can be achieved,
    but insertion sort is generally low. Effective, because insertion sorting can only move the data one place at a time
    . The basic idea of ​​Hill sorting is: first divide the entire sequence of records to be sorted into several subsequences for direct insertion sorting, and wait for the records in the entire sequence to be "basic". Ordered", then perform direct insertion sorting on all records in turn.

  3. Algorithm steps:

    1) Select an incremental sequence t1, t2, ..., tk, where ti>tj, tk=1;

    2) According to the number of incremental sequences k, sort the sequence k times;

    3) For each sorting, according to the corresponding increment ti, the sequence to be sorted is divided into several sub-sequences of length m, and the sub-tables are directly inserted and sorted. Only when the increment factor is 1, the entire sequence is treated as a table, and the table length is the length of the entire sequence.

    Graphical explanation of 8 major sorting algorithms





  4. selection sort

    Selection sort is also a simple and intuitive sorting algorithm.

    Algorithm steps:

    1)首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置

    2)再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。

    3)重复第二步,直到所有元素均排序完毕。

    Graphical explanation of 8 major sorting algorithms




  5. 冒泡排序

    冒泡排序(Bubble Sort)也是一种简单直观的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。

    算法步骤:

    1)比较相邻的元素。如果第一个比第二个大,就交换他们两个。

    2)对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。

    3)针对所有的元素重复以上的步骤,除了最后一个。

    4)持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

    Graphical explanation of 8 major sorting algorithms



  6. 归并排序

    归并排序(Merge sort)是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。

    算法步骤:

    申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列

    设定两个指针,最初位置分别为两个已经排序序列的起始位置

    比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置

    重复步骤3直到某一指针达到序列尾

    将另一序列剩下的所有元素直接复制到合并序列尾

    Graphical explanation of 8 major sorting algorithms



  7. 快速排序

    快速排序是由东尼·霍尔所发展的一种排序算法。在平均状况下,排序 n 个项目要Ο(n log n)次比较。在最坏状况下则需要Ο(n2)次比较,但这种状况并不常见。事实上,快速排序通常明显比其他Ο(n log n) 算法更快,因为它的内部循环(inner loop)可以在大部分的架构上很有效率地被实现出来。

    快速排序使用分治法(Divide and conquer)策略来把一个串行(list)分为两个子串行(sub-lists)。

    算法步骤:

    1 从数列中挑出一个元素,称为 “基准”(pivot),

    2 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作。

    3 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。

    递归的最底部情形,是数列的大小是零或一,也就是永远都已经被排序好了。虽然一直递归下去,但是这个算法总会退出,因为在每次的迭代(iteration)中,它至少会把一个元素摆到它最后的位置去。

    Graphical explanation of 8 major sorting algorithms



  8. 堆排序

    堆排序(Heapsort)是指利用堆这种数据结构所设计的一种排序算法。堆积是一个近似完全二叉树的结构,并同时满足堆积的性质:即子结点的键值或索引总是小于(或者大于)它的父节点。

    堆排序的平均时间复杂度为Ο(nlogn) 。

    算法步骤:

    1)创建一个堆H[0..n-1]

    2)把堆首(最大值)和堆尾互换

    3)把堆的尺寸缩小1,并调用shift_down(0),目的是把新的数组顶端数据调整到相应位置

    4) 重复步骤2,直到堆的尺寸为1

    Graphical explanation of 8 major sorting algorithms



  9. 基数排序

    基数排序是一种非比较型整数排序算法,其原理是将整数按位数切割成不同的数字,然后按每个位数分别比较。由于整数也可以表达字符串(比如名字或日期)和特定格式的浮点数,所以基数排序也不是只能使用于整数。

    说基数排序之前,我们简单介绍桶排序:

    算法思想:是将阵列分到有限数量的桶子里。每个桶子再个别排序(有可能再使用别的排序算法或是以递回方式继续使用桶排序进行排序)。桶排序是鸽巢排序的一种归纳结果。当要被排序的阵列内的数值是均匀分配的时候,桶排序使用线性时间(Θ(n))。但桶排序并不是 比较排序,他不受到 O(n log n) 下限的影响。 简单来说,就是把数据分组,放在一个个的桶中,然后对每个桶里面的在进行排序。

    例如要对大小为[1..1000]范围内的n个整数A[1..n]排序

    首先,可以把桶设为大小为10的范围,具体而言,设集合B[1]存储[1..10]的整数,集合B[2]存储 (10..20]的整数,……集合B[i]存储( (i-1)*10, i*10]的整数,i = 1,2,..100。总共有 100个桶。

    然后,对A[1..n]从头到尾扫描一遍,把每个A[i]放入对应的桶B[j]中。 再对这100个桶中每个桶里的数字排序,这时可用冒泡,选择,乃至快排,一般来说任 何排序法都可以。

    最后,依次输出每个桶里面的数字,且每个桶中的数字从小到大输出,这 样就得到所有数字排好序的一个序列了。

    假设有n个数字,有m个桶,如果数字是平均分布的,则每个桶里面平均有n/m个数字。如果

    对每个桶中的数字采用快速排序,那么整个算法的复杂度是

    O(n + m * n/m*log(n/m)) = O(n + nlogn – nlogm)

    从上式看出,当m接近n的时候,桶排序复杂度接近O(n)

    当然,以上复杂度的计算是基于输入的n个数字是平均分布这个假设的。这个假设是很强的 ,实际应用中效果并没有这么好。如果所有的数字都落在同一个桶中,那就退化成一般的排序了。

    前面说的几大排序算法 ,大部分时间复杂度都是O(n2),也有部分排序算法时间复杂度是O(nlogn)。而桶式排序却能实现O(n)的时间复杂度。但桶排序的缺点是:

    1)首先是空间复杂度比较高,需要的额外开销大。排序有两个数组的空间开销,一个存放待排序数组,一个就是所谓的桶,比如待排序值是从0到m-1,那就需要m个桶,这个桶数组就要至少m个空间。

    2)其次待排序的元素都要在一定的范围内等等。

  10. 各种排序的稳定性,时间复杂度、空间复杂度、稳定性总结如下图所示:

    关于时间复杂度:

    (1)平方阶(O(n^2))排序   各类简单排序:直接插入、直接选择和冒泡排序;

    (2) Linear logarithmic order (O(nlog2n)) sort, quick sort, heap sort and merge sort; (3) O(n^(1+§)) sort, § is a constant between 0 and 1. Hill sorting (4) Linear order (O(n)) sorting Radix sorting, in addition to bucket and box sorting.

    Regarding stability:

    Stable sorting algorithms: bubble sort, insertion sort, merge sort, and radix sort

    Not a stable sorting algorithm: selection sort, quick sort, hill sort, heap sort

    Graphical explanation of 8 major sorting algorithms

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324771306&siteId=291194637