Time complexity of various sorting algorithms

Source: Internet
Author: User

1. Quick Sort (QuickSort)

Quick Sort is an in-place sort, divide-and-conquer, large-scale recursive algorithm. Essentially, it is the in-place version of the merge sort. The quick sort can be made up of four steps below.

(1) If there is no more than 1 data, return directly.
(2) General selection of the leftmost value of the sequence as pivot data.
(3) The sequence is divided into 2 parts, part of which is larger than the pivot data, and the other part is smaller than the pivot data.
(4) Use recursion to sort the series on both sides.

Fast sorting is faster than most sorting algorithms. Although we can write algorithms that are faster than fast sorting in some special cases, there is usually no faster than that. Fast sorting is recursive, and it is not a good choice for machines with very limited memory.

2. Merge sort (mergesort)

The merge sort first breaks down the sequence to be sorted, from 1 to 2, 2 to 4, and then to a group of 1, which can be sorted and then merged back into the original sequence so that all the data can be sorted. A merge sort is slightly faster than a heap sort, but requires more memory space than the heap, because it requires an extra array.

3. Heap sequencing (heapsort)

Heap sequencing is suitable for situations where data volumes are very large (millions of data).

Heap ordering does not require a large number of recursive or multidimensional staging arrays. This is appropriate for a very large sequence of data volumes. For example, more than millions of records, because the fast sort, the merge sort all uses the recursive design algorithm, when the data volume is very big, may have the stack overflow error.

The heap sort will build all the data into a heap, the largest data at the top of the heap, and then exchange the heap top data and the last data of the sequence. Then rebuild the heap again, exchange the data, and then go down and sort all the data.

4.Shell Sorting (shellsort)

Shell sorting divides the data into groups, sorts each group first, and then inserts all the elements one at a time to reduce the number of times the data is exchanged and moved. The average efficiency is O (NLOGN). The rationality of grouping will have an important effect on the algorithm. Now use the D.e.knuth grouping method more.

The shell sort is 5 times times faster than the bubble sort, twice times faster than the insertion sort. Shell sorting is much slower than quicksort,mergesort,heapsort. But it is relatively simple, it is suitable for the amount of data under 5000 and speed is not particularly important occasions. It is very good to repeat the sequence of numbers with a smaller amount of data.

5. Insert Sort (insertsort)

Insert sort by inserting the values in the sequence into a sequence that is already sorted until the end of the sequence. Insert sort is an improvement to the bubbling sort. It's twice times faster than bubble sort. It is generally not necessary to use the insert sort when the data is greater than 1000, or to repeat the sequence of more than 200 data items.

6. Bubble sort (bubblesort)

Bubble sort is the slowest sort algorithm. It is the least efficient algorithm in practical application. It compares each element in the array by one trip to another, so that the larger data sinks and the smaller data Bandi. It is an O (n^2) algorithm.

7. Exchange sort (exchangesort) and select Sort (selectsort)

Both of these sorting methods are sorting algorithms for the switching method, and the efficiency is O (n2). In the actual application is in and bubble sort basically same position. They are just the initial stages of the development of sorting algorithms, which are less used in practice.

8. Base sort (radixsort)

The Cardinal sort and the usual sorting algorithm do not go the same way. It is a relatively novel algorithm, but it can only be used for the ordering of integers, if we want to apply the same method to floating point numbers, we must understand the storage format of floating-point numbers, and in a special way to map floating-point numbers to integers, and then mapping back, this is a very troublesome thing, therefore, its use is not much. And, most importantly, this algorithm also requires more storage space.

Reference: http://blog.csdn.net/likefrank/article/details/2974949

Time complexity 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.