Algorithm stability

Stability of the common sorting algorithm

edit
Heap sort , quick sort , shell sort , direct selection sort is unstable sorting algorithm and radix sort , bubble sort , direct insertion sort , binary insertion sort , merge sort is stable sorting algorithm.
First, the stability of the sorting algorithm we should know, before ordering simply is to ensure an equal number of which two are in the same position before and after the sort order and sequence of two positions before and after the sequence. In a simple formal look, if Ai = Aj, Ai original position before, after or to sort Ai Aj position before.
Secondly, he said about the benefits of stability. If the sorting algorithm is stable, then from a sort key, then another key from the sorted, ordering the first key result of the second key may be used to sort. Radix sort is so low press sorted sequentially by sorting high, like elements also lower high then the order will not change the same.
Back to the topic, now analyze the stability of the common sorting algorithm, each given a simple reason.
(1) bubble sort
Bubble sort is the little elements move it forward or backwards in the big elements. Comparison of two adjacent elements is more, also the exchange occurs between these two elements. So, if two elements are equal, I think you will not be bored to put the two of them exchanged a look; if not two equal adjacent elements, even if the front by pairwise exchange up to two adjacent, which when not switched, so that the same order before and after the element has not changed, so the bubble sort is a stable sorting algorithm.
(2) Selection Sort
Selection sort is to select the minimum of each current element location, such as to select the smallest first position, in which the remaining elements to the second element of a second small selection, and so on, until the n-1 elements, n elements do not choose, because it only under one of the largest element of. Then, the selected trip, if the current element is smaller than one element, while the smaller elements appeared in the back and one of the current element equal elements, then the stability of the exchange is broken. Mouthful, for example, the sequence 58529, we know that the first pass selects the first element 2 and 5 will be swapped, the original sequence order of the two opposite longitudinal 5 is broken, so sorting is not selected a stable sorting algorithm.
(3) insertion sort
Insertion sort is based on a small series have been ordered on time to insert an element. Of course, the beginning of this orderly sequence only a small element, is the first element. The comparison is an ordered sequence from the end of the beginning, that is, you want to insert elements and have been ordered to start than the greatest, if it is large than directly inserted behind it, or else looking straight ahead until you find the inserted it position. If a meet and insert elements are equal, then the insert elements like element inserted on the back element are equal. Therefore, before and after the equal element does not change the order, from the order of the original sequence disorder is out after the ordered sequence of rows, so that insertion sort is stable.
(4) quicksort
Quicksort two directions, the subscript i has been left right away, when a [i] <= a [center_index], where subscript center_index is the central element of the array, the array is generally taken to be the 0th element. And j index on the right has been left to go, when a [j]> a [center_index]. If i and j are walk, i <= j, exchanging a [i] and A [j], the above process is repeated until i> j. Switching a [j] and a [center_index], quicksort complete trip. And when the central element a [j] exchanged, is likely to upset the stability of the previous element, such as the sequence 53343891011, now the central elements 3 and 5 (the 5th element, the index starts count) exchange will disrupt the stability of the element 3, so that a quick sort is unstable sorting algorithm, instability occurs in the central element, and a [j] of the exchange timing.
(5) merge sort
Merge sort is to recursively divided into a sequence of short sequence, the short sequence recursion is only one outlet element (that directly ordered) or 2 sequence (compare and swap 1), then each ordered sequence of segments into a combined long sequence motif, constantly merging the original sequence until all sorted. It can be found, at one or two elements, a switching element does not, elements 2 of equal size if not deliberately switched, without destabilizing it. So, in short an ordered sequence of process of the merger, is subject to damage stability? No, the merger process, we can guarantee that if the current two elements are equal, we are in front of the sequence of elements stored in the front of the result of the sequence, thus ensuring stability. So merge sort algorithm sort is stable.
(6) radix sort
Radix sort is in accordance with the low first sorted and then collected; then sorted according to high, and then collect; and so on, up to the highest position. Sometimes there is some attribute priority order, first by prioritizing low, then high priority ranking, the final order is a high priority of the previous high, high priority same high low priority first. Radix sort are based sorting, collecting, so it is stable sorting algorithm.
(7) Hill sorting (shell)
Hill is sorted according to its elements do not synchronized long insertion sort, when started very unordered elements, steps maximum, so the number of elements inserted into the sort of small, fast; when the basic elements of an orderly, step length is small, insertion sort ordered sequence for a high efficiency. Therefore, the time complexity Hill sorting would be better than o (n ^ 2) number. Since multiple insertion sort, insertion sort we know that one is stable, it does not change the relative order of the same elements, but, like elements may move in a respective insertion sort, insertion sort different process, its stability will finally it is disrupted, so the shell sort is unstable.
(8) heapsort
We are aware of the heap structure is a child node i 2 * i and 2 * i + 1 nodes, the parent node requires a large stack top which is not less than 2 child nodes, the parent node of claim small top stack 2 or less which child nodes. In a sequence of length n, heap sort process and the beginning of 3 the maximum value of its child nodes selected (large stack top) from the n / 2 or a minimum (minor vertex stack), the choice between these three elements Of course not undermine stability. But for the n / 2-1, n / 2-2, ... 1 which is selected a parent element will be destabilizing. Possible of the n / 2 behind a parent node of the switching element switching passed, while the first n / 2-1 parent of a node of the same switching element is not behind, the stability between these two elements on the same It has been destroyed. So, heap sort is not a stable sorting algorithm.
In summary, it concluded: select sort, quick sort, shell sort, heap sort is not a stable sorting algorithm, and bubble sort, insertion sort, merge sort and radix sort is stable sorting algorithm.

Guess you like

Origin www.cnblogs.com/cstdio1/p/11111687.html