Stability comparison of various sorting algorithms for data structures

Source: Internet
Author: User
Tags data structures

first, there are 9 kinds of sorting algorithms mentioned above:

1. Simple selection of sorting

2. Heap sort (1 and 2 are select sort)

3. Direct Insert Sort

4. Hill sort (3 and 4 belong to the insertion sort, sometimes the improved direct insertion sort is called binary insertion)

5. Bubble sort

6. Quick Sort (5 and 6 are interchange sort.) The interchange sort is the name of the non-stop exchange of data locations. But in fact the choice of sorting is also constantly exchanging elements, but fewer times, Only one swap is found for the maximum value. The emphasis is also on the selection of the highest value by traversal or heap. The bubble sort is the maximum value by exchanging the neighboring elements, and the fast sort also keeps exchanging elements so that the sequence is close to order. Focus on switching)

7. Base sorting

8. Bucket Sorting (7 and 8 are assigned sort)

9. Merge sort

second, in the face of this multi-sorting algorithm you may be depressed to sort by yourself which algorithm is good? What scenarios are available for each algorithm?

In order to compare the above various algorithms, can be considered from the following aspects.

1. Stability of sorting algorithms

2. Sorting algorithm time complexity

3. Sorting algorithm space complexity

4. The best scenario for the various algorithms.

third, ranking algorithm stability
The so-called stability is that there are two elements equal in the sequence to be sorted, and their order is unchanged after sorting. If it's a1,a2. The indexes of A1,A2 after sorting are still 1 and 2.

Stability can also be understood as everything is in the grasp, the position of the element is in your control. The unstable algorithm sometimes has a bit of luck, a random ingredient. When two elements are equal, their positions may still be the same after sorting. But it may be different.

It is also important to note that the algorithm itself is independent of the programming language, so you write code to implement the algorithm when a lot of details can do different processing. Using an unstable algorithm no matter how you write the code when you implement it, the location of the same element is always indeterminate (the location may or may not change). And the stable ranking algorithm is you in the concrete implementation if the details of the good treatment will be stable, but some details are not processed by the results are still unstable.

For example, bubble sort, direct insert sort, merge sort is stable sorting algorithm, but if you implement the details are not well-handled results are also unstable.

Iv. The usefulness of stability

The test data we use when using sorting algorithms is simply some of the values themselves. There is no associated information. This is generally not useful in practical applications. The actual value of the sort should be associated to other information, such as the primary key ordering of a table in the database, The primary key is associated to other information. In addition to the English alphabet, the value of the English alphabet is associated with the meaningful message of the letter.

Perhaps most of the time we don't have to think about the stability of the algorithm. Two elements equal position is before is not important. But sometimes stability is useful. It embodies the robustness of the program. For example, you can rank the most popular articles or music movies on your website. Because the rankings here are not as good as our scores. So there's the element. There will be a number of points in the same order. If you add a new element, you'll have to re-rank it. The best thing to do is to still keep the sequence.

five, which algorithms are stable?
Stability algorithms: Cardinal Sort, direct insert sort, bubble sort, merge sort

Instability algorithm: Bucket sort, binary insert sort, hill sort, quick sort, simple select sort, heap sort

A variety of algorithmic stability detailed
(1) Cardinal sort (stable) and bucket sequencing (unstable)

Both of these algorithms belong to the allocation sort algorithm. (The information from the element value itself is mapped directly into an auxiliary sequence to become ordered. Instead of determining the order position by comparing it to other elements)

Cardinality sorting because in order to map the low level to a temporary sequence, it is in order to map, not related to the data position changes. Then the same element is mapped in order. So the same elements are mapped in order. So it's stable.

If the data elements are not duplicated, then the simple bucket is ordered, there is no duplicate elements, so there is no stable stability of the said. If there are duplicate elements to be sorted with an improved bucket. The secondary temporary array is simply indexed to identify the key value of the element to be sorted. Missing additional information ( This is the only algorithm in the algorithm that uses the auxiliary temporary sequence to lose information. If the element to be sorted is a map type, it is sorted by its key value. Other algorithms take the map type as an element when using auxiliary sequences. And only the improved bucket sort does not take the map type as an element, Only the key value information is used. This makes it impossible to differentiate between key and value information, so naturally it is an unstable algorithm.

(2) Merge sort (Stable)

The merge sort makes the recursive thought, divides the sequence recursively into the small sequence, and then merges the ordered subsequence. When the sequence of the left and right two sub-sequences are merged, the left sequence is usually first traversed, and if there is an equal element in the left side of the sequence, it is still in front This is stable. But if you have to go through the right sequence first, the algorithm becomes unstable. Although this sort of order is also true, it becomes unstable, so it is not very good implementation.

(3) Simple selection sequencing (unstable) and heap sequencing (unstable)

Both of these algorithms belong to the selection sort. (select the maximum or minimum value from the element to be sorted.) The following example takes the minimum value as an example)

Simple selection sort the position becomes unstable when the minimum value is chosen. For example 8 3 8 1. When the minimum value is traversed from left to right, 1 is found, which requires 8 and 1 to be swapped. So the position of two equal element 8 is changed.

Heap sorting, there will also be the same as above the exchange of the maximum position will lead to instability. For example, there are a large number of 8 8 6 5 2. First the maximum value of 8 is selected, and the last one is at the end. It is not stable at this time. Because the second 8 ran ahead of it.

(4) bubble sort (Stable) and quick sort (unstable)

Both of these algorithms belong to the interchange sort.

Bubbling is done by nonstop traversal, in ascending order, if the left side of the adjacent element is greater than the right side of the interchange. When you encounter an equal, you do not exchange and remain in place. So it is stable. Of course, if you have to eat enough to hold on, and when you meet equal, it will become an unstable algorithm.

Fast sorting is not stable. For example 8 5 6 6. On the basis of 8, the last 6 after the first trip to the first, 8 to the last. The second interchange. This 6 runs to the 5 position. It becomes orderly. Two 6 positions have changed, so it is unstable.

(5) Direct insert (Stable), binary insert sort (unstable) with Hill sort (unstable)

When you insert directly, you find the appropriate position in the sorted subsequence and then insert it. Suppose the left is sorted and the right is not sorted. By traversing the sorted sequence from backward forward, and then inserting, the equal element can still remain in place. But if you were to traverse the sorted sequence backwards, it would be an unstable sort.

Binary insertion ordering is unstable because the position obtained by binary lookup is not stable. For example 3 4 4 5 4. But when the last 4 is inserted, it will certainly run to the second 4. So it's unstable.

We can draw such a experience from the above analysis.
1. As long as the exchange between the positions of two elements is not involved, it is certainly a stable sorting algorithm. For example, merge sort, base sort. (Bucket sequencing instability is a special case, because it loses the accompanying information, otherwise it can be a stable sort of)

2. In the algorithm that involves the exchange of elements in different locations, the addition of bubbling and direct insertion sequencing is stable, and the others are unstable.

You can think of the reason why the same element position changed is one of the switching position from the other head jumped over, and the bubbling algorithm is adjacent position interchange, jump not past, encounter the same element when the stop is not exchanged.

The direct insertion sort is inserted into the ordered sequence. So you stop by walking backwards and forwards, so you can stay stable. But remember that you have to walk backwards, or it will be unstable. (So the direct insertion is semi-stable, and bubble is very stable, unless you have the pain of the egg must be two equal elements 22 exchange)

Reprint Address: http://blog.csdn.net/weiwenhp/article/details/8621049

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.