Problems related to sorting algorithms

Source: Internet
Author: User

Stability Problem of sorting algorithm

Before the written test, the interview, has been asked about the common ranking algorithm stability model problem, sometimes often is more choice, although have seen a lot of such problems, but each time is not allowed. In this summary, I hope to gain some.

First of all, the stability of the sorting algorithm should be known, popularly speaking is to be able to guarantee the sort,2 equal numbers of the sequence and after the order of their two before and after the order of the same position. Again simple image point, if ai=aj,ai originally in position before, after sequencing AI still want in Aj position before.

Second, describe the benefits of stability. If the sorting algorithm is stable, sort from one key and then sort from another, the result of sorting the first key can be used to sort the second key. The Cardinal sort is like this, first by the low sort, successively by the high order, the low level same element its order at the high level also is invariable. In addition, if the sorting algorithm is stable, the number of elements exchanged may be less (personal sense, not confirmed) for comparison-based sorting algorithms.

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

(1) Bubble sort

The bubble sort is to move the small element forward or the large element back. is a comparison of two adjacent elements , and the interchange also occurs between these two elements. So, if the two elements are equal without exchanging positions, and if the two equal elements are not adjacent, then even if two are adjacent by the preceding 22 interchanges, this time will not be exchanged, so the order of the same elements is not changed, so the bubble sort is a stable sort algorithm .

(2) Select sort

   Select Sort is the smallest of the current elements selected for each location, such as selecting the smallest one for the first position, and then selecting the second small one for the remaining elements, and so on, until then-1element, sectionNelement, because it has only one of its largest elements left. Then, in a trip, if the current element is larger than the following element, and the small element appears behind an element that is equal to the current element, then the post-swap stability is destroyed. A bit of a mouthful, for example, a sequence5 8 5) 2 9, we know the first time to select1an element5Will and2Exchange, then in the original sequence2a5the relative order is destroyed, so choosing a sort is not a stable sorting algorithm.

(3) Insert sort

  An insert sort is an element that is inserted one at a time, based on an already ordered small sequence . Of course, there are only 1 elements at the beginning of this ordered sequence , which is the first element. The comparison begins at the end of the ordered sequence, that is, the element that you want to insert and the largest person in order. If it is larger than it, insert it directly behind it, otherwise go straight ahead until you find it and insert the position. If you encounter an element equal to the insert, the insertion element places 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 the order of the sequence, so the insertion sort is stable .

(4) Quick Sort

The quick sort has two directions, the left oneISubscript always go right, whenA[i]<=a[center_index], whereCenter_indexis an array subscript for the central element, usually taken as0an element. And on the right.Jthe subscript always goes left, whenA[j]>a[center_index]. IfIand theJcan't move,i<=j, ExchangeA[i]and theA[j], repeat the process above and knowi>j. ExchangeA[j]and theA[center_index], complete a quick sort of trip. In the central element andA[j]In exchange, it is possible to disrupt the stability of the preceding elements, such as the sequence of5 3 3 4 3 8 9, now the central element5and the3(Section5elements, subscript from1start) The exchange will take the element3stability is disrupted, so fast sequencing is an unstable sorting algorithm that occurs in the central element andA[j]exchange of time.

(5) Merge sort

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), The ordered short sequences are then 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,2 Elements are not exchanged if they are equal in size, and this does not break the stability. So, in the process of merging short ordered sequences, is stability compromised? No, in the process of merging we can guarantee that if the current element is equal, we keep the previous sequence in front of the result sequence, which guarantees stability. Therefore, the merge sort is also a stable sorting algorithm.

(6) Base order

The cardinality sort is sorted by the 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 is high priority, high priority, low priority, high precedence. Base sorting is based on sorting separately and is collected separately, so it is a stable sorting algorithm.

(7) Hill sort

Hill sort is the insertion of elements according to different steps, 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 very low, and the insertion sort is very 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 one insertion sort is stable and does not change the relative order of the same elements, but during different insertion sorts, the same elements may move in their own insert sort and finally their stability is disrupted, so the shell The sort is not stable.

(8) Heap Sorting

We know that the structure of the heap is a nodeIthe child for2*iand the2*i+1node, the large top heap requires the parent node to be greater than or equal to its2child nodes, the small top heap requires that the parent node is less than or equal to its2child nodes. In a long-Nsequence, the process of sorting the heap is from the firstN/2start and its child nodes are all3Maximum (large top heap) or minimum (small top heap), which3the choice between elements is of course not to undermine stability. But when it isn/2-1,n/2-2,...... 1When these parent nodes select an element, the stability is broken. It is possible that the firstN/2A parent node exchange passes the latter element, and the firstn/2-1A parent node to put the back one the same without swapping, then this2The stability between the same elements is destroyed. So, the age ranking is not a stable sorting algorithm.

In conclusion, it is concluded that the sorting algorithm of sorting, quick sorting, hill sort, heap sorting is not stable, and the two bubbles sort, insert sort, merge sort and cardinal sort are stable sorting algorithms. There are some sorting algorithms that are not summarized and follow-up will continue ...

Problems related to sorting 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.