Stability and time complexity analysis of common sorting algorithms

Source: Internet
Author: User
Tags numeric value relative requires sort


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

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

2. What is the significance of studying the stability of sequencing algorithms?

First of all, the stability of the sorting algorithm should be known, popularly speaking is to ensure that the first two of the same sort of data in the sequence of the order of their orders and sorted after their order of two successive positions.

Again, if a i = = a j,ai originally before the AJ position, the Ai is still in front of the AJ position.

Let's examine the benefits of 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. See the essay, "Cardinal Sort" for details.

(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.

That means that although the "age" is sorted, the order of study numbers is still a small to large requirement.

3. Stability analysis of various sorting algorithms

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). 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.

So bubble sort is a stable sorting algorithm.

(2) Select sort

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 of the second position to select a small,

And so on, until the n-1 element, the nth element is not selected, because it is left with one of its largest elements.

Then, if the current lock element is larger than the next element in a trip, and the smaller element appears behind an element that is equal to the currently locked element, then the post-swap position order is clearly changed.

Oh. A bit of a mouthful, for example: Sequence 5 8 5 2 9, we know 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.

So the choice of sorting 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, 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

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 A[j] are exchanged, it is very possible to disrupt the stability of the preceding elements, such as sequence 5 3 3 4 3 8 9 10 11

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

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, stability is not 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.

Therefore, the merge sort is also a stable sorting algorithm.

(6) Base order

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

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 one insert sort is stable and does not change the relative order of the same elements.

However, during different insertion sorts, the same elements may move in the respective insert sort, and finally their stability will be disturbed.

So the shell sort is an unstable sort algorithm.

(8) Heap Sorting

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.

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.