I have written the test several times over the past few days, and I have encountered a series of questions about common sorting.AlgorithmThe problem of stability determination is often caused by multiple choices. It is not a question that can be easily concluded for me and those who are not as sure as I do.
However, if you have remembered which data structure books are stable and which ones are not stable before the written test, you can easily solve them. This article aims at keeping this in mind or trying to understand why it is stable.
Prepared by a fixed or unstable person.
First, we should all know the stability of sorting algorithms, in general, it can ensure that the first two equal numbers are sorted in the same order before and after the sequence. In a simple form, if AI = AJ, the AI is in the front of the location, and the sorted AI is still in the front of the Aj location.
Next, let's talk about the benefits of stability. If the Sorting Algorithm is stable, sort from one key and then from another key. The result of the first key sorting can be used by the second key sorting. Sort by base
In this case, the elements with the same low position are sorted by the low position, and those with the same low position are not changed at the same time. In addition, if the Sorting Algorithm is stable, for comparison-based sorting algorithms, element exchange
The number of times may be less (personal feeling, not confirmed ).
Go back to the topic and analyze the stability of common sorting algorithms. Each gives a simple reason.
(1) Bubble Sorting
Bubble Sorting is to call a small element forward or a large element backward. The comparison is an adjacent comparison between two elements, and the Exchange also occurs between these two elements. Therefore, if the two elements are equal, I think you will not have them again.
Exchange the two of them in a chat. If the two equal elements are not adjacent, they will not be exchanged even if they are adjacent through the two exchanges, the order of the same elements is not changed.
So Bubble Sorting is a stable sorting algorithm.
(2) Select sorting
The sorting mode selects the smallest element for each position. For example, you can specify the smallest element for the first position, select the second smallest element for the second element in the remaining element, and so on, n-1 element, nth
You do not need to select the element, because only one of its largest elements is left. If the current element is smaller than the current element, and the small element appears after an element equal to the current element
After switching, the stability is damaged. Compare interfaces. For example, the sequence is 5 8 5 2 9,
We know that the first selection of 1st elements 5 will exchange with 2, then the relative order of 2 5 in the original sequence will be damaged, so the selection of sorting is not a stable sorting algorithm.
(3) Insert sorting
Insert sorting inserts an element at a time based on an ordered small sequence. Of course, at the beginning, this ordered small sequence had only one element, which was the first element. The comparison is from the end of the ordered sequence.
Starting from the beginning, that is, the element to be inserted is directly inserted behind it if it is larger than that of the already ordered operator. Otherwise, you can continue to find the inserted position until you find it. If you encounter
So that the inserted element is placed behind the equal element. Therefore, the order of equal elements is not changed, and the order from the original unordered sequence is the order after sorting, so insertion sorting is stable.
.
(4) Fast sorting
The quick sorting has two directions. The I subscript on the left always goes to the right. When a [I] <=
A [center_index], where center_index is the array subscript of the central element. Generally, it is set to 0th elements of the array. The J subscript on the right goes to the left, when a [J]
> A [center_index]. If I and j cannot move, I <= J, exchange a [I] And a [J], repeat the above process until I> J.
Exchange a [J] And a [center_index] to complete a quick sorting. When the central element is exchanged with a [J], it is very likely that the stability of the preceding elements is disrupted, for example, the sequence is 5.
3 3 4 3 8 9 10 11,
Now the exchange of central elements 5 and 3 (5th elements, subscript starting from 1) will disrupt the stability of element 3, so fast sorting is an unstable sorting algorithm, instability occurs in central element and a [J]
The time of exchange.
(5) Merge and sort
Merge Sorting refers to recursively dividing a sequence into short sequences. The recursive exit means that a short sequence has only one element (that is, directly ordered) or two sequences (one comparison and exchange ), then combine the ordered segments into
The long sequence is merged until all the original sequence is sorted. It can be found that when one or two elements, one element will not be exchanged. If two elements are equal in size, no one will intentionally exchange them, which will not damage stability.
. So, in the process of merging short ordered sequences, is stability damaged? No. During the merge process, we can ensure that if the two current elements are equal, we save the elements in the previous sequence in the knot.
This ensures stability. Therefore, Merge Sorting is also a stable sorting algorithm.
(6) Base sorting
Base sorting is sorted first by low position, then collected; then sorted by high level, and then collected; and so on until the highest bit. Sometimes some attributes are sorted by priority first, and then by priority
First-level sorting, the last order is the highest priority, and the highest priority is the same as the highest priority. Base sorting is based on separate sorting and collected separately, so it is a stable sorting algorithm.
(7) Shell)
Hill sorting sorts elements by insertion of different step sizes. When the elements are unordered at the beginning, the step size is the largest, so the number of elements inserted for sorting is very small and the speed is very fast; when the elements are basically ordered and the step size is small,
Insert sorting is highly efficient for ordered sequences. Therefore, the time complexity of hill sorting is better than that of O (N ^ 2. Due to the multiple insertion sorting, we know that one insertion sorting is stable and will not change the same element.
The relative sequence of elements, but in different insert sorting processes, the same elements may move in their respective insert sorting, and the final stability will be disrupted, therefore, shell sorting is unstable.
(8) Heap sorting
We know that the heap structure is that node I has 2 * I and 2 * I + 1 nodes, and the parent node must be greater than or equal to its 2 child nodes, the child top heap requires that the parent node be smaller than or equal to its two child nodes. At a length of N
The heap sorting process starts from n/2 and selects the largest (large top heap) or least (small top heap) for the three values of its subnodes ), of course, the choice between these three elements will not undermine stability. But when n
/2-1, n/2-2,
... 1 when selecting elements for these parent nodes, stability will be compromised. It is possible that the nth/2nd parent node swaps the next element, while the nth/2-1 parent node does not
If there is an exchange, the stability between the two identical elements will be damaged. Therefore, heap sorting is not a stable sorting algorithm.
In conclusion, we can conclude that the selection of sorting, fast sorting, Hill sorting, and heap sorting are not stable sorting algorithms, bubble sorting, insert sorting, Merge Sorting, and base sorting are stable sorting algorithms. I have not performed any sort algorithms.