Stability analysis of common algorithms

Source: Internet
Author: User

The stability of the sorting algorithm everyone should know, popularly speaking is to ensure that the first two equal data in the sequence of the order of their sequential position and after the order of their two successive positions of the same sequence. That is: if a i = = a J,ai originally before the AJ position, the Ai is still in front of the AJ position.

1. Brief summary

Select sort, quick sort, hill sort, heap sort not a stable sorting algorithm

Bubble sort, insert sort, merge sort, and Cardinal sort are all stable sorting algorithms.

2. The significance of ranking algorithm stability

(1) If the sorting algorithm is stable, sort from one key and then sort from another, the result of the first key sort can be used for ordering the second key. The Cardinal sort is this, first by the low sort, successively by the high order, then the low-level same data element its order of precedence is not changed even if the high level is the same.

(2) When learning the sorting principle, the elements that may be ordered in the program are simple types, and actually when applied, it is possible to sort the array of a complex type (custom type) . the sorted key value is just one attribute of this element, and for a simple type, the numeric value is its full meaning, even if the exchange is not different.

However, in the case of complex types, the exchange of words may cause an exchange of elements that should not otherwise be exchanged. For example: A "student" array, in order to sort by age, "student" This object not only contains "age", there are many other attributes. if the original array is the number of the primary key from small to large data collation. And the stable sort will guarantee the comparison, if two students of the same age, will not exchange. It also means that although the "age" is sorted, the order of study numbers is still a small to large requirement.

(3) If the sorting algorithm is stable, the number of elements exchanged may be relatively small for comparison-based sorting algorithms.

3. Stability analysis of various sorting algorithms

(1) Bubble sort: stable

The bubble sort is to move the small element forward (or the large element back). Note that there are two adjacent elements to compare, and whether swapping is required also occurs between these two elements. So, if two elements are equal, I guess you won't be bored to swap them out again. If the two equal elements are not adjacent, then even if the two elements are adjacent by the preceding 22 interchange, they will not be exchanged at all, so the order of the same elements is not changed after sorting them.

(2) Select sort: unstable

Select Sort to select the element that is currently the smallest in the element to be sorted for each location. For example, to the first position to choose the smallest, in the remaining elements to the second position to select the next small, and so on, until the n-1 element, the nth element is not selected, because it is only one of the largest element. Then, when you select, if the current lock element is larger than the next element, and then the smaller element appears after the element that is equal to the currently locked element, the post-swap position order is obviously changed.

For example: Sequence 5 8 5 2 9, we know that the first trip to select the 1th element 5 will be exchanged with 2, then the original sequence of two 5 of the relative order is destroyed.  

(3) Insert sort: stable

An insert sort is an element that is inserted one at a time, based on an already ordered small sequence. of course, at first, this ordered small sequence has only 1 elements, the first element (which is ordered by default).

The comparison starts at the end of the ordered sequence, that is, the element to be inserted is compared to the one that is already ordered, and if larger than it is inserted directly behind it. Otherwise, keep looking forward until you find the location where it should be inserted. If you meet an element equal to the insert, place the element you want to insert behind the equal element.

So, the order of the equal elements is not changed, the order from the original unordered sequence is still in the order of sequence, so the insertion sort is stable.

(4) Quick sort: unstable

The quick sort has two directions, the I subscript on the left goes right (when the condition A[i] <= A[center_index]), where Center_index is the array subscript of the central element, which is generally taken as the No. 0 element of an array.

and the right J subscript goes left (when a[j] > A[center_index]). If I and J are not moving, I <= J, Exchange A[i] and A[j], repeat the process above 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] are exchanged, it is possible to disrupt the stability of the preceding elements, such as the sequence 5 3 3 4 3 8 9, now the central elements 5 and 3 (the 5th element, the subscript starting from 1) Exchange will be the stability of element 3 is disrupted.

So the fast sort is an unstable sorting algorithm, and instability occurs at the moment of central element and A[j] Exchange.

(5) Merge sort: stable

The merge sort is to divide the sequence recursively into short sequence, the recursive exit is the short sequence only 1 elements (think directly ordered) or 2 sequences (1 comparisons and exchanges), then the ordered sequence of segments is combined into an ordered long sequence, which is continuously merged until the original sequence is fully sequenced. It can be found that when 1 or 2 elements, 1 elements are not exchanged, and 2 elements are equal in size and no one is intentionally exchanged, which does not destabilize the stability. So, in the process of merging short ordered sequences, is stability compromised? No, we can guarantee that if the two current elements are equal, we keep the elements in the preceding sequence in front of the result sequence, which guarantees stability.  

(6) Cardinal rank: stable

The cardinality sort is sorted by low, then collected, sorted by high order, then collected, and so on, until the highest bit.

Sometimes some properties are prioritized, sorted by low priority, then sorted by high priority, and the final order result is high priority in front, high priority in the same case, low priority.

Base sorting is based on sorting separately and is collected separately, so it is a stable sorting algorithm.

(7) Hill sort: unstable

Hill Sort is to follow the different steps to insert the elements of the order, when the first element is very disordered, the step is the largest, so the number of elements inserted in the order is very small, fast; when the elements are basically ordered, the step size is small, and the insertion order is highly efficient for ordered sequences. So, the time complexity of hill sorting would be better than O (n^2). because of the number of insertions, we know that once the insertion sort is stable and does not change the relative order of the same elements , the same elements may move in their own insertion sort during different insertion sorts, and eventually their stability will be disrupted.

(8) heap sequencing: unstable

we know that the heap structure is the child of node I for the 2*i and 2*I+1 nodes, the large top heap requires that the parent node is 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 n, the process of heap sequencing is to select the largest (large top heap) or the smallest (small top heap) from the beginning of N/2 and its child nodes by a total of 3 values, and the choice between the 3 elements will of course not destabilize.

but when for n/2-1, N/2-2, ... 1 When these parent nodes select an element, the stability is broken. It is possible that the N/2 of the parent node Exchange passes the latter element, while the n/2-1 parent node does not exchange the subsequent same element, then the stability between the 2 identical elements is destroyed.

Stability analysis of common algorithms

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.