Several internal sorting-classification-complexity-stability

Source: Internet
Author: User

1. Brief description

This paper mainly describes the classification, complexity and stability of some commonly used internal sorting algorithms. Mainly based on the current understanding and learning, detailed and accurate complexity can be found in Wikipedia and other more authoritative sites, for some of the different implementations of the algorithm, the complexity is also different, the complexity given here is relatively good complexity of the algorithm.

2. Classification

3. Complexity and stability

Bubble sort: Gets the complexity of O (N) in an already ordered situation.
Quick sorting: Each recursive is n complexity, the number of recursion is related to the sequence, and when ordered, recursion n times, Time complexity O (N*LOGN)
Insert sort: Gets the complexity of O (N) in an already ordered situation.
Hill sort: The worst time and average time are judged by the step array, and now the worst time complexity for the algorithm is O (N*LOGN). Space is primarily required for step arrays. There is no common factor between general steps.
Insert Sort: The number of interchanges is much less than the bubble sort, because the swap requires more CPU time than the comparison requires more CPU time, the selection sort is faster than the bubble sort when the N value is small.
Heap sort: Heap sort belongs to select Sort, select N-1 times, select one from the heap each time, so the worst is n*logn. The best time can not be accurately estimated, basically also n*logn this degree.
Merge sort: The complexity of the sorting algorithm is independent of the array initialization sequence, the number of merges is LOGN times, each complexity is O (N), so the complexity is O (N*LOGN)
Radix sort: K is the maximum number of digits in an array, numeric. for int, use decimal word, k<=10. In terms of stability, cardinal ordering requires a stable sort to be used each time, otherwise the result will not be correct at the end. So the stability here is necessary, and there is no possibility of instability. Other sorts, such as bubble sort, can be changed to be unstable, which requires attention.
Count Sort: K is the range of values in the array, that is, k=max-min+1. for int, the worst case is k=2^32, where the maximum positive and minimum negative numbers are included in the array.
Bucket sort: Because the bucket sort may have no data, then assume N data, only be divided into M barrels, O (N) +o (m* (n/m) *log (n/m)) =o (n+n* (LOGN-LOGM)) =o (N+N*LOGN-N*LOGM) =o (n+n* ( LOGN-LOGM)), the more time the barrel is divided, the closer N. The count sort can be seen as a special case of bucket sequencing, where buckets are opened very large, until each different value is guaranteed to be divided into a single bucket.

4. Reference

Wikipedia
Three kinds of linear sorting algorithm counting sort, bucket sort and cardinal sort http://www.byvoid.com/blog/sort-radix/
Bucket sort and Base sort http://anwj336.blog.163.com/blog/static/8941520920109535025216/

Tags: algorithm

Several internal sorting-classification-complexity-stability

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.