Sorting Algorithm Summary

Source: Internet
Author: User
Sorting Algorithm Summary

It took a long time to finally learn the basics of sorting. During this time, I learned a lot and summarized it:
Sort by learning Algorithm There are: insert sorting, Merge Sorting, Bubble sorting, select sorting, Hill sorting, heap sorting, fast sorting, Count sorting, base sorting, and bucket sorting (not implemented ). Compare the learning experience.
I am not very clear about their time complexity, and I really don't know who they are, who is slow, because the derivation in the book is really just a little bit of understanding, and it is not digested. They do not fully understand their essence, so they still need expert advice on any mistakes. Haha.
1. Popularize sorting stability. The so-called sorting stability means that if the two numbers are the same, the sorting results for them will not change their relative order. For example, a = { 1 , 2 , 1 , 2 , 1} after sorting here is a = { 1 , 1 , 1, 2 , 2 } "Stability" means that the first 1 after sorting is the first 1 Before sorting, the second 1 is the second 1 Before sorting, and the third 1 is the third 1 Before sorting. Likewise, the same is true for 2. Color is used here. Unstable, that is, their order should not be the same as the starting order. That is, it may be a = { 1 , 1, 1 , 2 , 2 .
2. Popularize in-situ sorting: In-situ sorting refers to sorting without applying for extra space, that is, comparing and exchanging the original sort data. For example, fast sorting and heap sorting are all in-situ sorting, Merge Sorting, and count sorting.
3. Who is the best? In my opinion, quick sorting is the best. Time Complexity: N * log (N), unstable sorting. In-situ sorting. His name is great, fast. Of course it's coming soon. I think his thinking is very good, divide, and sort in situ, saving a lot of space waste. The speed is also very fast, N * log (n ). However, there is a weakness: if the time complexity is N * n, but the random addition also improves, and he can make any comparison, as long as you can give the relationship between the two elements. Wide application range and fast speed.
4. Insert sorting: time complexity of N * n, stable sorting, and in-situ sorting. Insert sorting is the first sort I learned, and the speed is still very fast. Especially after the array has been sorted, It is very efficient to insert a data with its idea. Because not all rows are required. There are very few data exchanges, but they only move the data back and put it into the data to be inserted. (This does not mean calling insert sorting, but uses its idea ). I think most of the data is sorted, and sorting with insert will bring you great convenience. There are very few moves and exchanges of data.
5. Bubble sorting, time complexity of N * n, stable sorting, and in-situ sorting. The idea of Bubble Sorting is very good. One by one comparison, the small move up, in turn to determine the current minimum element. Because of its simplicity, stable sorting, and good implementation, it is more useful. Another point is that after the Sentinel is added, he can exit early.
6. Select sorting, time complexity of N * n, stable sorting, and in-situ sorting. Selecting sorting is the basic idea of bubbling. from an early stage, you can choose one by one until the selection ends. This is the opposite process of insertion sorting. insertion is to determine the position of an element, and selection is to determine the position of the element. The advantage is that only definite elements are selected at a time and a lot of data is not exchanged. Therefore, the data exchange volume should be smaller than the bubble volume.
7. Insert sorting, select sorting, and compare Bubble sorting. Their time complexity is N * n. I think their efficiency is almost the same. I personally like bubble, because most of the data is not enough when using it, and the sorted array can be returned in advance. Even if the other two are sorted, they need to perform all scans. In terms of data exchange, bubble is indeed more than all of them. Haha. The example shows that inserting a data is sorted after the end, and only one bubble operation can be done, and selection and insertion must be done with N * n complexity. It depends on what you think.
8. Merge Sorting: the time complexity of N * log (N), stable sorting, and non-in-situ sorting. His idea is to divide and conquer, first into small parts, and then merge. Because we apply for another space, the efficiency of merging is 0 (n. Fast. It seems that the upper limit is N * log (N), so if it is a comparison, it is less than the quick sorting. All arrays can be effectively sorted in N * log (n. However, although he is not sorted in the original order, although he is very fast, it seems that his popularity is not quickly sorted.
9. Heap sorting: time complexity of N * log (N), non-stable sorting, and in-situ sorting. His idea is to use the heap data structure. The heap can be regarded as a Complete Binary Tree, so the number of comparisons in sorting can be very small. In addition, it is also in the same order, and does not need to apply for additional space, and the efficiency is also good. However, his thoughts are more difficult to grasp than they are. Another is to add a data reorder based on the sorted order. The number of exchanges and comparisons will not be reduced at all. Although heap sorting is not widely used in fast sorting, its data structure and ideas are really good, and its priority queue is used for efficiency. Heap, we still need to learn and master it well.
10. Hill sorting: time complexity of N * log (N) (This is incorrect. It should be n ^ Lamda (1 <Lamda <2). Lamda is related to the step selection .) , Non-stable sorting, in-situ sorting. The main idea is divide and conquer. However, the split and merge sort are different. Instead of merging the left half and right half, the split is grouped by step. The START step is half of the length. Divided into nlen/2 groups, and sorted by group. The next step is reduced to half the original step and sorted by group until the step is 1. After sorting, the hill sorting is completed. This idea is good. It is said that it is an upgraded version of the insert sort, So I intentionally used the insert sort when sorting each group. I think he is a very good sorting method. The disadvantage is that the two numbers may be multiple times, because the two data will be divided multiple times, but they will not exchange data. Efficiency is also very high.
11. quick sorting, heap sorting, Merge Sorting, and comparison of hill sorting, all of which are complex in time are N * log (N). I think quick sorting is the most widely used, although his sorting is unstable, in many cases, he doesn't care whether he is stable or not. His comparison times are relatively small, because he divides the data into two parts: big and small. Each time, A number is determined, so theoretically there will be no two numbers that are compared twice. In the end, data is exchanged, and there are very few data exchanges. Merging sorting and heap sorting also have these advantages, but merging sorting requires additional space. The heap sorting Heap has already prepared much faster data exchange than the data exchange. Therefore, quick sorting is widely used. It is also easy to grasp and implement.
12. Counting sorting: time complexity of N, stable sorting, and non-in-situ sorting. His idea is quite novel, that is, he first agreed that the data range is not very large, and the data is all integers (or can be located to an integer), and then directly applied for a space. Match the element value of array a to be sorted with the subscript of the requested Space B, and store the number of element values of the subscript in array B to locate the position of each element in array. In this way, the efficiency is only N. Because it is quite special, although it is very fast, there are not many places to use.
13. Base sorting: time complexity of N, stable sorting, and non-in-situ sorting. His idea is that data is concentrated in a specific range. For example, data is 4-digit, all are 5-digit, or data has multiple keywords. We will start from you and then rank 10, rank to the highest bit in sequence, because we can use an n method to rank one, so the total complexity of the method is D * n. The same is true for keywords. We first rank 3rd keywords, 3rd keywords, and the first keyword. Only when the time complexity of each keyword is ensured to be completed at N, the entire sorting is a D * n time complexity. So the overall speed is very fast. However, make sure that the keyword can be completed in n time complexity.
14. Bucket sorting: time complexity of N, stable sorting, non-in-situ sorting. The main idea is the same as that of base sorting. It is also assumed that the values are in the same range, for example, the probability is between 0 and 1, and the distribution is quite even, in this way, we divide a number into the specified area like the base sorting. Then you can connect to these areas. The book uses linked list storage for each area. I think there will be time in the small area. So it's just the theoretical N-time complexity. This idea is good. Haha.
15. Counting sorting, base sorting, and comparison of Bucket sorting. I think they all have great ideas, but they can achieve the best results under specific circumstances. Although efficient, it is not widely used. I prefer counting and sorting between them. I can find my position directly by ing. In theory, the sum of base sorting and same sorting is only the n-time complexity. the ordering of a keyword in base sorting is n complexity, and the sorting of each region in bucket sorting is n complexity.
16. Last sentiment of sorting algorithms: the existence of sorting algorithms is reasonable. Therefore, these sorting algorithms are very good, and he does give us ideological help. Thanks to our predecessors for leaving the essence to us. I have gained a lot. I would like to summarize the gains of their respective sorting:
Bubble mode: This mode is easy to implement and can be used for lightweight data sorting.
Insert sorting: small data sorting is also used, but I learned how to insert a data from his mind. In this way, you know that you don't have to sort the data in the data, but you just need to call insert directly.
Select sorting: I learned how to obtain the maximum and minimum values. You only need to select one.
Merge Sorting: I learned the divide and conquer method, and it is very suitable for merging two arrays.
Heap sorting: it can be used to achieve priority queues, and his idea should add a lot of internal forces to me.
Quick sorting: the sorting that is used most often is not helpful to me.
Hill sorting: this is also a sub-governance, which makes me see the different sub-governance. It turns out that there is such an idea.
Count sorting, base sorting, and bucket sorting: Special Handling in special cases.
Attach the connections I learned to sort here

Quick Sort Learning 1 Quick Sort learning 2 (randomization Version)

Quick sorting learning 3 (initial edition) quick sorting learning 4 (initial edition and random Edition)

Insert sort bubble sort select sort

Hill sort merge sort Heap Sort use heap to implement priority queue

Sort by base sort count

posted on shongbee2 reading (2867) Comments (5) edit the category of the reference to the favorite: Data Structure and 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.