Analysis of various sorting algorithms and its Java implementation

Source: Internet
Author: User

Sorting can be divided into two categories: inner and outer sort. During the sorting process, all records are stored in memory, called internal sorting, and if external memory is required during sorting, it becomes an outer sort.
The following categories are available in the ordering:

    • Insert class Sort: Direct insert sort, dichotomy (binary) Insert sort, hill (shrink increment) sort
    • Select class Sort: Simple select sort, heap sort
    • Exchange class Sort: bubble sort, quick sort, three-way quick sort
    • Sort by Merge class: Two-way merge sort
    • Base class ordering: MSD cardinality ordering, LSD cardinality ordering
Comparison of various internal sorting algorithms

Direct insert sorting, bubble sorting, and simple selection sorting are the basic sorting methods. Their average time complexity is O (N2), and their implementations are very simple.
The direct insert sort is very effective for small-scale sequence of elements (n≤25). Its time complexity is related to the initial arrangement of the sequence of elements to be sorted. In the best case, the direct insert sort can be done with a n-1 comparison and no swap is required. In the average and worst case, the comparison and exchange of the direct insert sort are O (N2).
Improved bubble sorting can be done in the best case with only a single trip to the bubbling process, and only a n-1 comparison is required.
The sort code comparison of the simple selection sort is independent of the initial arrangement of the sequence of elements to be sorted, and the number of comparisons is always O (N2), but the number of elements moved is related to the initial arrangement of the sequence of elements to be sorted, preferably without moving at the same time, and the number of elements moved less than 3 (n-1) times.
In terms of memory complexity, these three basic sorting methods do not require additional memory space in addition to an auxiliary element. In terms of stability, the direct insert sort and bubble sort are stable, but the simple selection of sort is not. They are mainly used in cases where the number of elements n is not very large (<10k).
Fast sorting is the most versatile and efficient internal sorting algorithm, with an average time complexity of O (nlog2n), and in general the additional memory required is O (log2n). But fast sorting is an unstable algorithm. And in some cases may degenerate (for example, when the sequence of elements is ordered), the time complexity increases to O (N2), and the spatial complexity increases to O (n). The improved fast sorting algorithm, i.e. the three-way fast sorting algorithm, can effectively avoid the worst-case occurrence.
Heap ordering is also an efficient internal sorting algorithm with a time complexity of O (nlog2n), and there is no worst-case scenario that causes heap sequencing to run significantly slower, and heap ordering does not require additional memory space. But heap sorting is unlikely to provide better average performance than a quick sort. Heap ordering is an unstable sorting algorithm.
Merge sort is also only an important efficient sorting algorithm its performance is independent of the sequence of input elements, and the time complexity is always O (nlog2n). But its main disadvantage is that direct execution requires O (n) of additional memory space, although there are ways to overcome this shortcoming, but the cost is that the algorithm is complex and time complexity will increase, so in practical applications is generally not worth it. Merge sort algorithm is a stable and efficient sorting algorithm.
Fast sorting, heap sorting, and merge sorting are suitable for situations where the number of elements is n large.
The time complexity of hill sequencing is between the basic sorting algorithm and the efficient sorting algorithm, although the analysis of its performance to date is still imprecise, but the hill sort code is simple, does not need any extra memory, the space complexity is low. Hill sort is an unstable sorting algorithm. For a medium-sized sequence of elements (n≤1000), Hill Sorting is a good choice.
Radix sorting is a kind of relatively special sort algorithm, which not only compares the ordering code of the sequence of elements, but also deals with the different parts of the sorting code. While cardinality sequencing has a linear increase in time complexity, because in a regular programming environment, the internal loop of the keyword Index program contains a large number of operations, which are much larger than the internal loops of a quick sort or merge sort algorithm. So the linear time overhead of the cardinality sort is not actually much smaller than the time overhead of fast sorting. And because the sorting code extraction algorithm based on the base sort is affected by the operating system and the ordering element, its adaptability is far less than that of ordinary comparison and exchange operation. Therefore, in the actual work, the general efficient sorting algorithm, such as the application of fast sequencing, is much more extensive than the base order.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Analysis of various sorting algorithms and its Java implementation

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.