Data structure--comparison of various sorting algorithms

Source: Internet
Author: User
Tags sorts

1. Comparison of time complexity
Considering the average time complexity, direct insert sort, bubble sort, direct select sort are three simple sorting methods, time complexity is O (N2), and the time complexity of quick sort, heap sort, two-way merge sort is O (nlog2n), and the complexity of hill sort is between the two . When considering the best time complexity, it is best to insert sort and bubble sorting directly, with O (n), and the other best cases are the same as average. when considering the worst time complexity, the quick Sort is O (N2), the direct insert sort, the bubble sort, the hill sort is the same as the average, but the coefficients increase by about one times, so the running speed will be reduced by half, and the worst case has little effect on direct selection sorting, heap sorting, and merge sorting.
2. Comparison of spatial complexity
The spatial complexity of the merge sort is the largest, O (n), the spatial complexity of the fast sorting is O (log2n), and the other sort is O (1).
3. From the stability comparison
Direct Insert sort, bubble sort, merge sort are stable sorting methods, and direct selection of sort, hill sort, quick sort, heap sort is an unstable sort method.
4. Comparing the simplicity of the algorithm
The direct insertion sort, the bubble sort, the direct choice sort are the simple sorting method, the algorithm is simple, is easy to understand, but the hill sorts, the quick sorting, the heap sorts, the merge sorts all is the improved sort method, the algorithm is more complex than the simple sorting, also difficult to understand.

So far, the existing sequencing method is far more than the methods discussed in this chapter, people are keen to study a variety of sorting methods, not only because of the importance of sorting in the computer, but also because different methods have their advantages and disadvantages, can be applied to different occasions. the factors to consider when choosing a sorting method are :
The number of records to be sorted n; the size of the information in the record itself, the structure and distribution of the keywords, the requirements for the ordering stability, the conditions of the language tools, the size of the auxiliary tables, and so on. Based on these factors, the following conclusions can be drawn:
(1) If n is small (for example, N50), direct insertion or direct selection can be used. Because the direct insertion sort requires the record move operation more directly than the choice of sorting, so if the record itself information is large, then choose the direct selection of the appropriate sort.
(2) If the initial state of the file is in accordance with the basic order of keywords, then the selection of direct insertion sorting bubble is advisable.
(3) If n is larger, it should be based on its time complexity to choose the sorting method: Fast sorting \ Heap Sorting or merge sort, fast sorting is currently based on internal sorting is considered the best method, the document to sort the keyword is random, the average time for fast sorting is the least, but the heap sort requires less secondary space than the fast ordering, And there will be no worst-case scenario in order, both of which are unstable, and if ordering is stable, merge sort can be chosen. However, this article combined with the introduction of the 22 merge algorithm is not worth advocating, it can usually be combined with direct sorting. The sub-file that is evaluated by the direct insertion Order is then merged 22. Since the direct insert sort is stable, the improved merge sort is stable.
(4) The sorting algorithm discussed above, in addition to sorting, is implemented on a one-dimensional array, when the record itself is large, in order to avoid wasting time to move records, you can use the linked list as a storage structure, such as inserting sort and merge sort are easy to implement on the list, and called Table and merge table, but some methods, such as quick sorting and heap sorting, is difficult to implement on a linked list, in which case you can extract the keyword to create an index table, and then sort the index table. The simpler approach, however, is to introduce a shaping vector as a secondary table, before sorting, if the sorting algorithm requires an interchange, then simply Exchange R[i] and R[j], and after the sort is finished, the vectors indicate the order relationship between records.

Data structure--comparison of various 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.