Stable sort and unstable sort of __ sorting algorithm

Source: Internet
Author: User

Select sort, fast sort, hill sort, heap sort is not a stable sort algorithm, while bubble sort, insert sort, merge sort and cardinal order are stable sorting algorithms.

First of all, the stability of the sorting algorithm we should all know, in layman's terms is to ensure that the first 2 equal number of the sequence before and after the order and after the ordering of the order of their two before and after the same position. In the simple formalization, if Ai = Aj,ai originally in position, after the sort AI still want to be in Aj position before.

Second, talk about the benefits of stability. If the sort algorithm is stable, it is sorted from one key and then from another, and the result of the first key sort can be sorted by the second key. The Cardinal sort is this, first by the low order, successive orders by the high order, the lower the same level of the same sequence of the same will not change. In addition, if the sorting algorithm is stable, the number of element exchanges may be less (personal, unproven) for the comparison based sorting algorithm.

Back to the topic, now analyze the stability of the common sorting algorithm, each given a simple reason.

(1) Bubble sort

Bubble sort is to move small elements forward or to adjust the large elements backward. The comparison is a two-element comparison, and the exchange occurs between the two elements. So, if two elements are equal, I think you will not be bored to swap them out again; if two equal elements are not adjacent, then even if the previous 22 exchange to the two adjacent, this time will not be exchanged, so the same element before and after the order has not changed, so bubble sort is a stable sort algorithm.

(2) Select sort

The select sort is the smallest of the current element selected for each location, for example, for the first position to choose the smallest, in the remaining elements of the second element to select the second small, and so on, and so on, and so on, and so on, and so on, until the n–1 element, the nth element is not selected, because there is only one of the largest elements. Then, in a choice, if the current element is smaller than an element, and the small element appears behind an element that is equal to the current element, then the stability of the exchange is corrupted. Compared to a mouthful, for example, the sequence 5 8 5 2 9, we know that the first time to select the 1th element 5 and 2 Exchange, then the original sequence 2 5 of the relative order is destroyed, so the selection is not a stable sorting algorithm.

(3) Insert sort
The insertion sort is based on an already ordered small sequence that inserts one element at a time. Of course, at the beginning of this ordered sequence of only 1 elements, is the first element. The comparison begins at the end of the ordered sequence, where the element that you want to insert and the one that is already ordered is the one that is inserted directly behind it, if it is larger than it is, or until you find where it should be inserted. If you encounter an element that is equal to the insertion element, the insertion element places the elements that you want to insert behind the equality element. Therefore, the order of the equal elements is unchanged, the order from the original unordered sequence is the order after the sequence, so the insertion sort is stable.

(4) Quick Sort
The quick sort has two directions, the left I subscript goes right, and when A[i] <= A[center_index], where Center_index is the array subscript for the central element, typically the No. 0 element. And the J subscript on the right always goes left, when a[j] > A[center_index]. If I and J are not going to move, I <= J, swap A[i] and a[j], repeat the above process until I > J. Exchange A[j] and A[center_index] to complete a quick sort of a trip. When the central element and the a[j] exchange, it is possible to disrupt the stability of the elements in the front, for example, the sequence is 5 3 3 4 3 8 9 10 11, now the central elements 5 and 3 (the 5th element, subscript from 1) Exchange will disrupt the stability of element 3, so fast sorting is an unstable sort algorithm, Instability occurs at a time when central elements and a[j] exchange.

(5) Merge sort
Merge sort is to divide the sequence recursively into short sequences, the recursive exit is a short sequence with only 1 elements (considered direct order) or 2 sequences (1 comparisons and exchanges), and then merges each ordered sequence into an ordered long sequence, merging until the original sequence is all sorted out. It can be found that in 1 or 2 elements, 1 elements do not exchange, 2 elements if the size is equal and no one intentionally exchanged, this does not destroy stability. Then, in the process of merging short sequential sequences, the stability is destroyed. No, we can guarantee that if two current elements are equal, we keep the elements in the preceding sequence in front of the result sequence, which guarantees stability. Therefore, the merging sort is also a stable sorting algorithm.

(6) Cardinal order
The cardinality sort is sorted by the lows, then collected, sorted by high order, then collected, and so on until the highest bit. Sometimes some attributes are in order of precedence, first by low priority, and then by high priority, the final order is high priority in front, high priority of the same low priority high in the front. Base ordering is based on separate sorting and is collected separately, so it is a stable sort algorithm.

(7) Hill sort (shell)
Hill sorting is to insert the elements according to the different step size, when the first element is very disordered, the step is the largest, so the number of inserted sorted elements is very small, fast; When the elements are basically ordered, the step size is very low, the insertion order is efficient for ordered sequences. So the time complexity of hill sorting is better than O (n^2). Because of the multiple insertion sort, we know that a single insert sort is stable and does not change the relative order of the same elements, but in different inserts, the same elements may move in their respective insertion sort, and their stability will be disrupted, so the shell sort is unstable.

(8) Heap Sorting
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 that the parent node be less than or equal to its 2 child nodes. In a sequence of long n, the process of heap sequencing is to select the maximum (large top heap) or the smallest (small top heap) with a total of 3 values from the beginning of N/2 and its child nodes, and the choice between these 3 elements certainly does not break the stability. But when for n/2–1, N/2–2, ... 1 When these parent nodes select elements, they break the stability. It is possible that the N/2 of the parent node swaps the subsequent element, while the n/2–1 parent node does not exchange the same element, then the stability between the 2 identical elements is corrupted. Therefore, heap sorting is not a stable sort algorithm.

Welcome to visit my blog omegaxyz.com

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.