Sorting algorithm collation (AHA algorithm)

Source: Internet
Author: User

First, bucket sort this is a very fast sorting algorithm. Barrel sequencing has been used since the 1956, and the basic idea of the algorithm is proposed by E.J.ISSAC and R.c.singleton.      As I said before, actually this is not really a bucket sorting algorithm, the real bucket sorting algorithm is more complex than this. Example: Ordering of the array {5, 3, 5, 2, 8} (the maximum known value does not exceed 10)

The code is as follows:

  

Summary: The sorting is fast, but the performance loss is very large, the unknown size of the array can not be used, and the floating point type is very difficult to sort.

The basic idea of the classic sort bubble bubble sort is to compare two adjacent elements each time, and exchange them if they are in the wrong order. The code is as follows:

Summary: Classic sorting algorithm, two for loop, temporary variable change array position so as to achieve sorting purposes; Thirdly, the fast sort bubble sort can be said that we learn the first real sorting algorithm, and solve the problem of bucket sequencing waste space, but in the algorithm execution efficiency is sacrificed a lot of time complex     To O (n²).     If our computer can run 1 billion times per second, then the 100 million numbers are sorted, the bucket sort only takes 0.1 seconds, and the bubble sort takes 10 million seconds to 115 days, isn't it scary? Is there a sort algorithm that doesn't waste space and can be faster? That's "quick sort"!      Do you think it's very high-end to listen to this name? Example: array {6, 1, 2, 7, 9, 3, 4, 5, 10, 8} ideas: Start probing at both ends.     First find a number less than 6 from right to left, then find a number greater than 6 from left to right, then swap them.     Here you can use two variables I and J, pointing to the leftmost and rightmost of the sequence, respectively.     We have a nice name for these two variables "Sentinel I" and "Sentinel J". At first let Sentinel I point to the leftmost part of the sequence (ie I =1), pointing to the number 6.       Let Sentinel J point to the far right of the sequence (that is, j = 10), pointing to the number 8. Solution:

  

 

 

Example code: Summary: In fact, the quick sort is based on a thought called "dichotomy".notes:fast sorting is faster because each exchange is a jump-through compared to a bubbling sort. Every time I sort.sets a datum point that places a number less than or equal to the datum point to the left of the Datum point, and a number greater than or equal to the datum pointto the right of the datum point. This will not be the same as bubble sort in each exchange, only between the number of adjacentExchange , the distance is much greater. Therefore, the total comparison and the number of exchanges is less, the speed naturally increased. WhenHowever, in the worst case, it is still possible to exchange the two number of adjacent numbers. So the worst time complexity of fast sorting andThe bubble sort is the same, all O (N²), and its average time complexity is O (N logn).

Sorting algorithm collation (AHA 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.