**Document directory**
- Which algorithms are stable?

Nine sorting algorithms are mentioned earlier:

**1. Simple selection and sorting 2. Heap sorting**(1 and 2 are selected for sorting)

**3. Insert sort directly 4. Hill sort**(3 and 4 are insertion sorting. Sometimes the improved direct insertion sorting is called binary insertion)

**5. Bubble Sorting 6. Fast sorting**(5 and 6 are exchange sorting. as the name suggests, the exchange sorting is a non-stop data exchange location. but in fact, the selection of sorting is also constantly changing elements, but the number of times is small, only to find the maximum value is exchanged once. the focus is to select the most value through traversal or heap. in Bubble sorting, the maximum value is obtained by constantly exchanging adjacent elements, and the fast sorting keeps switching elements so that the sequence is closer to the order step by step. focuses on exchange)

**7. Base sorting 8. Bucket sorting**(7 and 8 belong to the allocation order)

**9. Merge Sorting**

In the face of this multi-sort algorithm, you may be depressed about which algorithm should be used for sorting by yourself? What scenarios are applicable to each algorithm?

To compare different algorithms, you can consider the following aspects.

1. Stability of sorting algorithms

2. Time Complexity of sorting algorithms

3. spatial complexity of sorting algorithms

4. Optimal scenarios for various algorithms.

Sorting Algorithm Stability

Stability means that two elements in the sequence to be sorted are equal, and their order remains unchanged after sorting. for example, A1 and A2. their indexes are respectively 1 and 2. after sorting, the indexes of A1 and A2 are still 1 and 2.

Stability can also be understood as that everything is under control and the element is in your control. the unstable algorithms sometimes have some luck, random components. when the two elements are equal, their positions may remain the same after sorting. but it may be different. is unknown.

Note that the algorithm idea itself is independent from the programming language, so many details can be processed differently when you write code to implement the algorithm. no matter how you write code when using an unstable algorithm, the final location of the same element is always uncertain (or the location may change ). the stable sorting algorithm is stable if you handle the details well during the specific implementation, but some details are not processed and the results are still unstable.

For example, Bubble sorting, insert sorting directly, and Merge Sorting are stable sorting algorithms. However, if the details are not processed properly, the results are unstable.

**Stability**

The test data we use when using the sorting algorithm is a simple number. no association information. this is generally not useful in practical applications. in fact, it should be pertinent that the value of sorting is associated with other information, such as the primary key sorting of a table in the database, and the primary key is associated with other information. for example, for sorting English letters, English letters are associated with meaningful information such as letters.

Most of the time, we don't need to consider algorithm stability. the equal position of the two elements is not important before and after. but sometimes stability is useful. it reflects the robustness of the program. for example, you rank the most popular articles or music movies on your website. because the ranking here will not be like the number of the rankings in our rankings. therefore, when the elements are equal, there will also be successive points. if a new element is added, it will be ranked again. it is better to keep the order of the rankings in parallel.

Which algorithms are stable?

**Stability algorithm:**Base sort, insert sort directly, bubble sort, merge sort

**Instability algorithm:**Bucket sorting, binary insertion sorting, Hill sorting, quick sorting, simple selection sorting, and heap sorting

Detailed explanation of various algorithm Stability

**(1) Base sorting (stable) and bucket sorting (unstable)**

Both algorithms belong to the allocation sorting algorithm. (The information of element values is directly mapped to an auxiliary sequence and becomes ordered, rather than determining the sequence position by comparing with other elements)

The base sorting is to map the low position to a temporary sequence in order, which does not involve data location changes. then map the data in the high order. therefore, the same elements are mapped in order. so it is stable

If there are no repeated data elements, the simple bucket sorting is used. At this time, there are no repeated elements, so there is no stability or instability. improved bucket sorting is required if duplicate elements exist. in this case, the secondary temporary array only uses indexes to identify the key values of the elements to be sorted. other information is lost (this is the only algorithm that uses the auxiliary temporary sequence to lose information ). if the elements to be sorted are of the map type, they are sorted by their key values. other algorithms use the map type as an element when using the secondary sequence. only the improved bucket sorting does not regard the map type as an element, but only uses the key value information. in this way, the information with the same key value cannot be distinguished, so it is naturally an unstable algorithm.

**(2) Merge and sort (stable)**

Merging and sorting makes the recursive idea, divides the sequence recursively into small sequences, and then merges the sorted subsequences. when the Left and Right subsequences of a sequence are merged, the left sequence is first traversed. If the left and right sequences contain equal elements, the left sequence is still in the front, which is stable. however, if you have to traverse the right sequence, the algorithm becomes unstable. although this sorting is correct, it becomes unstable, so it is not a good implementation.

**(3) Simple selection of sorting (unstable) and heap sorting (unstable)**

Both algorithms belong to the selected sorting. (select the maximum or minimum values from the elements to be sorted. The following example uses the minimum value as an example)

Simple selection and sorting because the minimum value needs to be switched, the location becomes unstable when it changes. for example, 8 3 8 1. when the minimum value is retrieved from the left-to-right traversal, 1 is found, and 8 and 1 need to be swapped. in this way, the positions of two equal elements 8 are changed.

In heap sorting, there will also be locations with the same swap maximum value as above, leading to instability. for example, there are a bunch of 8 8 6 5 2. select the first maximum value of 8 and put it at the end. this is unstable. because the second 8 will run before it.

**(4) Bubble Sorting (stable) and fast sorting (unstable)**

Both algorithms belong to exchange sorting.

Bubble is a non-stop traversal. Taking ascending order as an example, if the left of an adjacent element is greater than the right, it is exchanged. when equal conditions are met, they are not exchanged and kept in the original position. so it is stable. of course, if you have to be full, and exchange them when they are equal, it will definitely become an unstable algorithm.

Fast sorting is unstable. example 8 5 6 6. take 8 as the benchmark. After the first exchange, the last 6 goes to the first place, 8 to the last. second exchange. this 6 to 5 position. it becomes orderly. the two six positions have changed, so they are unstable.

**(5) Direct insertion (stable), binary insertion sorting (unstable), and Hill sorting (unstable)**

During Direct insertion, the proper position is first found in the sorted subsequence before insertion. if the left side is sorted, the right side is not sorted. by traversing the sorted sequence from the back to the front, and then inserting it, the equal element can still maintain the original position. however, if you traverse the sorted sequence from the past to the back, it will be unstable.

Binary insertion sorting is unstable because the obtained position is unstable during binary search. for example, 3 4 4 5 4. but when you insert the last 4, it will definitely go to the front of the second 4. so it is unstable.

Through the above analysis, we can come up with such an experience.

1. As long as the exchange of positions between two elements is not involved, it must be a stable sorting algorithm. For example**Merge Sorting and base sorting**. (The bucket sorting is not stable because it loses the attached information. Otherwise, it can be sorted stably)

2. In addition**Bubble and direct insertion sorting**It is stable, and others are unstable.

You can think like this, the reason why the position of the same element changes is that one of the switching positions jumps over the other head, while the bubble algorithm is interchangeable between adjacent positions and cannot jump over, when it comes to equal elements, it stops and does not change.

Direct insertion sorting is to insert data to the sorted sequence. so you can stop when you encounter an equal value by traversing from the back to the front, which can also maintain stability. but remember to traverse from the back to the front, otherwise it will be unstable. (**So it's semi-stable to insert directly, and bubble is very stable.**Unless you are idle and have to exchange the elements of the two phases)