Why the sorting algorithm requires stability

 

 

Heap sorting , quick sorting , Hill sorting , and direct selection sorting are unstable sorting algorithms, while radix sorting , bubble sorting , direct insertion sorting , half insertion sorting , and merge sorting are stable sorting algorithms.

First of all, everyone should know the stability of the sorting algorithm. In layman's terms, it can ensure that the first two equal numbers in the sorting sequence have the same order of their positions before and after the sorting. In a simple formalization, if Ai = Aj, Ai was originally in front of the position, after sorting Ai still has to be in front of the position of Aj.

Secondly, talk about the benefits of stability. If the sorting algorithm is stable, then sort from one key, and then sort from another key. The result of the first key sort can be used for the second key sort. The cardinality sorting is like this, first sorting by low order, and successively sorting by high order. If the order of the same low order elements is the same in the high order, the order will not change.

 

 

Assume that there are multiple records with the same key in the sequence of records to be sorted. If sorted, the relative order of these records remains unchanged, that is, in the original sequence, r[i]=r[j], and r [i] is before r[j], and in the sorted sequence, r[i] is still before r[j], this sorting algorithm is said to be stable; otherwise it is called unstable.

 

For an unstable sorting algorithm , just cite an example to illustrate its instability; for a stable sorting algorithm, the algorithm must be analyzed to obtain stable characteristics. It should be noted that whether the sorting algorithm is stable is determined by the specific algorithm. An unstable algorithm can become a stable algorithm under certain conditions, and a stable algorithm can also become unstable under certain conditions. algorithm.

For example, for the following bubble sorting algorithm, which was originally a stable sorting algorithm, if the condition of record exchange is changed to r[j]>=r[j+1], two equal records will exchange positions, thus changing Into an unstable algorithm.

1

2

3

4

5

6

7

8

9

10

11

void BubbleSort(int r[ ], int n){

    exchange=n; //第一趟冒泡排序的范围是r[1]到r[n]

    while (exchange) //仅当上一趟排序有记录交换才进行本趟排序

    {

        bound=exchange; exchange=0;

            for (j=1; j if (r[j]>r[j+1]) {

                r[j]←→r[j+1];

                exchange=j; //记录每一次发生记录交换的位置

            }

     }

}

For another example, quick sort was originally an unstable sorting method, but if there is only one group of records with the same key code in the records to be sorted, and the selected axis value happens to be one of the same key codes, then the quick sort is It is stable.

 

 

eg. 

The students in a class have been sorted according to their student number. I now require that they be sorted from youngest to oldest. If they are of the same age, they must be sorted in descending order of student number.
So the question is, if the age sorting method you choose is unstable, will the student ID of a group of students of the same age be messed up after sorting? You have to take this group of students of the same age and take a photo again according to their student ID . If it is a stable sorting algorithm, I just need to sort it by age.
It seems that the stable sorting algorithm saves time. The advantages of stability are realized

 

 

 

Detailed example:

 

 

First of all, everyone should know the stability of the sorting algorithm. In layman's terms, it can ensure that the first two equal numbers in the sorting sequence have the same order of their positions before and after the sorting. In a simple formalization, if Ai = Aj, Ai was originally in front of the position, after sorting, Ai still has to be in front of the position of Aj.

 

Secondly, talk about the benefits of stability. If the sorting algorithm is stable, then sort from one key, and then sort from another key. The result of the first key sort can be used for the second key sort.

 

(1) Bubble sort

Bubble sorting is to adjust small elements forward or adjust large elements backward. Comparison is the comparison of two adjacent elements, and exchange also occurs between these two elements. Therefore, if two elements are equal, I think you will not exchange them boringly; if two equal elements are not adjacent, then even if the two elements are adjacent to each other through the previous two-by-two exchange, this There is no exchange at the time, so the order of the same elements has not changed, so bubble sorting is a stable sorting algorithm.

 

 

(2) Selection sort

Selection sort is to select the smallest element for each position, such as the smallest for the first position, the second smallest for the second element among the remaining elements, and so on, until the n-1 element, the first There is no need to select n elements, because only its largest element is left. Then, in one selection, if the current element is smaller than an element, and the smaller element appears behind an element equal to the current element, the stability after the exchange is destroyed.

 

More mouth-watering, for example, the sequence 5 8 5 2 9. We know that selecting the first element 5 in the first pass will exchange with 2, then the relative order of the two 5s in the original sequence is destroyed, so the selection order is not A stable sorting algorithm.

 

 

 

(3) Insertion sort

Insertion sort is to insert one element at a time on the basis of an already ordered small sequence. Of course, at the beginning, this small ordered sequence has only one element, which is the first element. The comparison starts from the end of the ordered sequence, that is, the element that you want to insert is compared with the largest one that is already ordered. If it is larger than it, it will be inserted directly behind it, otherwise it will be searched forward until it finds the one that should be inserted. position.

 

If you encounter one that is equal to the inserted element, then the inserted element puts the element you want to insert after the equal element. Therefore, the order of equal elements has not changed, and the order from the original unordered sequence is the order after sorting, so the insertion sort is stable.

 

 

(4)Quick sort

There are two directions for quick sorting. The i subscript on the left goes all the way to the right. When a[i] <= a[center_index], center_index is the array subscript of the center element, which is generally taken as the 0th element of the array. And the j subscript on the right goes all the way to the left, when a[j]> a[center_index]. If i and j can't 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 sort.

 

When the central element is exchanged with a[j], it is very likely to disrupt the stability of the previous element. For example, the sequence is 5 3 3 4 3 8 9 10 11, and now the central elements 5 and 3 (the fifth element, Subscripts start from 1) Exchange will disrupt the stability of element 3, so quicksort is an unstable sorting algorithm, and the instability occurs at the moment when the central element and a[j] are exchanged.

 

 

(5) Merge sort

Merge sort is to recursively divide the sequence into short sequences. The recursive exit is that the short sequence has only 1 element (think of direct order) or 2 sequences (1 comparison and exchange), and then merges the various ordered segments into one sequence. The long sequence of the sequence is continuously merged until the original sequence is all sorted. It can be found that in the case of 1 or 2 elements, 1 element will not be exchanged, and if the two elements are equal in size, no one deliberately exchanges it. This will not destabilize the stability.

 

Then, in the process of merging short orderly sequences, is the stability undermined? No, during the merging process, we can guarantee that if the two current elements are equal, we save the element in the previous sequence in front of the result sequence, which ensures stability. Therefore, merge sort is also a stable sorting algorithm.

 

 

(6) Cardinality sort

Cardinality sorting is to sort according to the low order first, and then collect; then sort according to the high order, then collect; and so on, until the highest order. Sometimes some attributes have a priority order, first sorted by low priority, then sorted by high priority, the final order is high priority and high priority, high priority with the same low priority and high priority first. Cardinality sorting is based on sorting separately and collecting separately, so it is a stable sorting algorithm.

 

 

(7) Hill sort (shell)

Hill sorting is to insert and sort the elements according to the asynchronous length. When the elements are very disordered at the beginning, the step is the largest, so the number of elements in the insertion sort is very small and the speed is very fast; when the elements are basically in order, the step The length is small, and insertion sort is very efficient for ordered sequences. Therefore, the time complexity of Hill sorting is better than O(n^2). Due to multiple insertion sorts, we know that an insertion sort is stable and will not change the relative order of the same elements. However, during different insertion sorts, the same elements may move in their respective insertion sorts, and finally their stability will be Disrupted, so the shell sort is unstable.

 

 

(8) Heap sort

We know that the structure of the heap is that the children of node i are 2 * i and 2 * i + 1 nodes. The large top heap requires the parent node to be greater than or equal to its 2 child nodes, and the small top heap requires the parent node to be less than or equal to its 2 child nodes. In a sequence of length n, the process of heap sorting is to choose the largest (large top heap) or smallest (small top heap) from the n/2 and its child nodes with 3 values, the choice between these 3 elements Of course it will not destroy stability. But when selecting elements for n/2-1, n/2-2, ... 1, these parent nodes, the stability will be destroyed. It is possible that the n/2-th parent node swapped the next element, and the n/2-1th parent node did not swap the next same element, then the stability between these two same elements Was destroyed. Therefore, heap sort is not a stable sorting algorithm.

 

 

In summary, it is concluded that selection sorting, quick sorting, Hill sorting, and heap sorting are not stable sorting algorithms, while bubble sorting, insertion sorting, merge sorting and radix sorting are stable sorting algorithms.

 

Unstable sorting algorithms are: a bunch of Hill quick picks.

 

Guess you like

Origin blog.csdn.net/nyist_yangguang/article/details/113618219