Eight sorting (stability discussion)

Source: Internet
Author: User

Selecting sorting, fast sorting, Hill sorting, and heap sorting are not stable sorting algorithms, while Bubble sorting, insert sorting, Merge Sorting, and base sorting are stable sorting algorithms.

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. In this case, the base sorting is performed first by the low position, and then by the high position. The order of the elements with the same low position and the high position will not change at the same time. In addition, if the Sorting Algorithm is stable, for comparison-based sorting algorithms, the number of element exchanges may be less (personally, 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 be bored to exchange them. If the two equal elements are not adjacent, even if the two are adjacent through the previous two exchanges, at this time, the sequence of the same elements is not changed, so the Bubble Sorting is a stable sorting algorithm.

During sorting, the largest element will be moved to the right side like a bubble, and the larger element will be handed over to the right side using a method similar to the half-phase element, therefore, large elements will be moved to the right without stopping until the initial position.

The basic Bubble sorting method can use the flag Method for a little less time than the average time. When the batch column is completed, no manual operation is triggered, this indicates that the sorting has been completed, and there is no need to perform the subsequent round comparison and interaction operations. For example:

Before sorting: 95 27 90 49 80 58 6 9 18 50

  1. 27 90 49 80 58 6 9 18 50 [95] 95 surfaced
  2. 27 49 80 58 6 9 18 50 [90 95] 90 surfaced
  3. 27 49 58 6 9 18 50 [80 90 95] 80 surfaced
  4. 27 49 6 9 18 50 [58 80 90 95] ......
  5. 27 6 9 18 49 [50 58 80 90 95] ......
  6. 6 9 18 27 [49 50 58 80 90 95] ......
  7. 6 9 18 [27 49 50 58 80 90 95] As a result, there will be no further transactions, and the sorting will end early

In the above example, we also added a concept, that is, when I and I + 1 were not submitted, it indicates that the next I + 2 to N has been sorted, which increases the efficiency of the Bubble sorting.

(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, the n-th element does not need to be selected until the n-th element, because only one of its largest elements is left. If the current element is smaller than an element, and the small element appears after an element equal to the current element, then the stability will be damaged after the exchange. Compare interfaces. For example, in the sequence 5 8 5 2 9, we know that the first selection of 1st elements 5 will exchange with 2, therefore, the relative order of the two 5 in the original sequence is damaged. Therefore, selecting sorting is not a stable sorting algorithm.

Split the objects to be sorted into two parts. One is sorted, the other is unordered, and the other is selected as the minimum value from the unordered part of the backend, and put it in the last one of the sorted parts of the front end, for example:


Before sorting: 70 80 31 37 10 1 48 60 33 80

  1. [1] 80 31 37 10 70 48 60 33 80 selected minimum value 1
  2. [1 10] 31 37 80 70 48 60 33 80 selected minimum 10
  3. [1 10 31] 37 80 70 48 60 33 80 selected minimum 31
  4. [1 10 31 33] 80 70 48 60 37 80 ......
  5. [1 10 31 33 37] 70 48 60 80 80 ......
  6. [1 10 31 33 37 48] 70 60 80 80 ......
  7. [1 10 31 33 37 48 60] 70 80 80 ......
  8. [1 10 31 33 37 48 60 70] 80 80 ......
  9. [1 10 31 33 37 48 60 70 80] 80 ......

(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 starts from the end of the ordered sequence, that is, the element to be inserted is compared with the already ordered sequence. If it is larger than it, it is directly inserted after it, otherwise, search until you find the inserted position. If you encounter an element that is equal to the inserted element, the inserted element is placed behind the element that you want to insert. 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.

For example, if you play poker, we divide the cards into two groups. Each time we pull the front-end cards from the back, then insert the cursor position of the previous pile of cards, for example:


Before sorting: 92 77 67 8 6 84 55 85 43 67

  1. [77 92] 67 8 6 84 55 85 43 67 insert 77 before 92
  2. [67 77 92] 8 6 84 55 85 43 67 insert 67 before 77
  3. [8 67 77 92] 6 84 55 85 43 67 insert 8 before 67
  4. [6 8 67 77 92] 84 55 85 43 67 insert 6 before 8
  5. [6 8 67 77 84 92] 55 85 43 67 insert 84 before 92
  6. [6 8 55 67 77 84 92] 85 43 67 insert 55 Before 67
  7. [6 8 55 67 77 84 85 92] 43 67 ......
  8. [6 8 43 55 67 77 84 85 92] 67 ......
  9. [6 8 43 55 67 77 84 85 92] ......

(4) Fast sorting
There are two directions for quick sorting. The I subscript on the left is always directed to the right. When a [I] <= A [center_index], center_index is the array subscript of the central element, it is generally set to an array of 0th elements. 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 to disrupt the stability of the preceding elements. 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 when the central element is exchanged with a [J.

(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, the ordered segments are merged into an ordered long sequence until all the original sequences are 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 store the elements in the previous sequence before the result sequence, thus ensuring 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 have a priority order. They are first sorted by low priority and then by high priority. The final order is the highest priority, and the highest priority is 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, the step size is very small, and insertion sorting is very efficient for ordered sequences. Therefore, the time complexity of hill sorting is better than that of O (N ^ 2. Because of the multiple insertion sorting, we know that one insertion sorting is stable and does not change the relative order of the same elements. However, in different insertion sorting processes, the same elements may move in their respective insert sorting, and the final stability will be disrupted, so 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. In a sequence with a length of N, the heap sorting process starts from n/2 and selects the maximum value (large top heap) or the minimum value (small top heap) for its subnodes ), of course, the choice between these three elements will not undermine stability. However, when selecting elements for the n/2-1, n/2-2,... 1 parent nodes, the stability will be damaged. It is possible that the nth/2nd parent node swaps the next element, while the nth/2-1 parent node does not swap the next element, then the stability between the two identical elements is damaged. Therefore, heap sorting is not a stable sorting algorithm.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.