Common internal sorting of data structures

write in front

For a sorting algorithm, the advantages and disadvantages of the algorithm are generally measured from the following three aspects:

》Time complexity: mainly to analyze the number of comparisons of keywords and the number of recorded movements

》Space complexity: how much auxiliary memory is required for the analysis sorting algorithm

"Stability: If the keywords of the two records A and B are equal, but the order of A and B remains unchanged after sorting, the sorting algorithm is said to be stable; otherwise, it is unstable.


Sorting is divided into internal and external sorting based on:

If the entire sorting process does not require external memory, and all sorting operations are done in memory, it is internal sorting

Commonly used internal sorting categories

》Selection sort: direct selection sort, heap sort

》Exchange sort: bubble sort, quick sort

》Insertion Sort: Direct Insertion Sort, Half Insertion Sort, Shell Sort

》Merge Sort

》Sequentially

》Barrel Sort

》Radix sort


selection sort

Just find the smallest data in this comparison, and then exchange it with the first data in this comparison.

For direct selection sorting, assuming that there are n data, the number of data exchanges is at most n-1 times, but the number of comparisons of the program is large (the number of exchanges is unnecessary).

Overall, its time efficiency is O(n^2)

According to code result: direct selection sort is unstable

 

heap sort

Minimum heap (all nodes in the tree are less than the value of its left and right children) and maximum heap (all nodes in the tree are greater than the value of its left and right children), corresponding to a complete binary tree respectively

For a data group of n data elements, heap sorting needs to build a heap for n-1 times, and the function of each heap building is to select the maximum or minimum value of the heap. The essence of heap sort is a selection sort

 

The key to heap sorting is to build a heap

1. Starting from the last non-leaf node, compare the value of the node and its two child nodes; if a child node is greater than the value of the parent node, swap the parent node with the larger child node

2. Adjust forward step by step until the root node, that is, ensure that the value of each parent node is greater than or equal to the value of its left and right child nodes, and the heap building is completed.

 

For heap sorting, assuming there are n pieces of data, n-1 times of heap building is required, and each heap build takes log2,n; then its time efficiency is O(n* log2,n )

Heapsort is very space efficient, it only requires one additional program unit for swap, and its space efficiency is O(1)

From the result of heap sort, it is not stable

 

Bubble Sort

Bubble sort is one of the most well-known exchange sorts. It has the characteristics of simple algorithm idea and easy implementation.

After each exchange of bubble sort, it can not only squeeze the current maximum value out of the rearmost position, but also partially straighten out other elements in front; once no exchange occurs in a certain pass, the sorting can be ended in advance.

 

The time efficiency of the bubble sort algorithm is uncertain. In the best case, if the initial sequence is already in an ordered state, it is enough to perform one bubbling, that is, do n-1 comparisons without swapping.

But in the worst case, the initial data sequence is in a completely reversed state, the algorithm needs to perform n-1 times of bubbling, and the i-th pass does ni comparisons,

Bubble sort is very space efficient, it requires only one additional program unit for swap, and is O(1)

Bubble sort is stable

 

quicksort

is a very fast swap sort algorithm

The basic idea is relatively simple:

From the data sequence to be sorted, one data (such as the first data) is used as the dividing value, all data elements smaller than it are placed on the left, and all data elements larger than it are placed on the right, after such a trip Next, the sequence forms two subsequences, and then the left and right subsequences are recursively, and the central element is reselected for the two subsequences and adjusted accordingly, until there is only one element left in each subsequence, and the sorting is completed.

Quick sort only has two exchanges to make the data to the left of the cutoff value smaller than the cutoff value, and the data to the right of the cutoff value to be greater than the cutoff value, so the speed is very fast.


快速排序的时间效率很好,因为它每趟能确定的元素成指数级增长

快速排序需要使用递归,而递归使用栈,因此它的空间效率为O(log2.n)

另一方面,快速排序这种包含跳跃交换,因此是不稳定的排序算法


插入排序

》直接插入排序

》shell排序

》折半插入排序


直接插入排序

思路很简单:将待排序的数据元素按其关键字的值的大小插入前面的有序序列

直接插入排序的时间效率并不高,在最坏的情况下,所有 元素的比较次数总和为(0+1+2。。。+n-1)=O(n^2)

在其他情况下,也要考虑移动元素的个数,故时间复杂度为O(n^2)

直接插入排序的空间效率很好,他只需要一个缓存数据单元,空间效率为O(1)

稳定的


折半插入排序

折半插入排序是对直接插入排序的简单改进

对于折半插入排序而言,当第i-1趟需要将第i个元素插入前面的0~i-1个元素序列中,他不会直接从第i-1个元素开始比较:

1.计算0~i-1索引的中间点,也就是用i索引处的元素和(0+i-1)/2索引处的元素来进行比较,如果i索引处的元素大,就在另一半的范围内搜索,这就是所谓的折半。

2.不断的折半,将搜索范围缩小到1/2,1/4,1/8.从而快速确定第i个元素的插入位置。

3.确定插入位置后,程序将位置以后的元素整体后移一位,然后将第i个元素放入该位置。

稳定的


shell排序

希尔排序对直接插入排序进行了改进:它通过加大插入排序中元素的间隔,并在这些有间隔的元素中进行插入排序,从而使数据项大跨度的移动。

通过创建这些交错的内部有序的数据项集合,就可以减少直接插入排序中数据项”整体搬家“的工作量


希尔排序比插入排序快很多,当间隔即增量h大的时候可以减少很大的移动量,因此效率很高。

shell排序是直接插入排序的改进版,因此也是稳定的,它的空间开销也是O(1)

时间开销大致是O(n的3/2次方)到n的7/6次方)之间


归并排序

”合并排序“:将两个有序的数组进行合并,成为一个新的有序大数组。

归并算法需要递归地进行分解,合并,每进行一趟归并排序需要调用merge(、)方法一次,每次执行merge()算法都要比较n次,因此归并排序算法的时间复杂度是O(n*log2.n)

归并算法的的空间效率比较差,它需要一个与原始序列同样大小的辅助序列

归并排序算法是稳定的


桶式排序

不是一个基于比较的排序方法这种排序方式必须满足两个特征:

》待排序的所有值在一个可枚举范围内

》待排序的可枚举范围不应太大,否则排序开销太大


桶式排序算法是一种极其优秀的算法,时间效率极高,它只需要两轮遍历即可:

第一轮遍历待排数据,统计每个待排数据落入各个桶中的个数;

第二轮遍历用于重新计算每一个桶里的数组元素的值

两轮遍历后就可得到每个待排数据的在有序序列中的位置,然后将每一个数据依次放入指定位置即可

桶式排序的空间开销较大,他需要两个数组

桶式排序数稳定的


Guess you like

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