Simple three sort

Source: Internet
Author: User

Bucket sequencing: The idea is to apply a sufficient amount of space to store the values that may appear, loop all the values, place the corresponding worthy places to mark, and then sort the order according to the value of the output.

Example: 2 5 8 6 1 4

Request Array A[8]

appear a number, a[that number] = 1;

The output of the last loop array, with a value of 1.

Bubble sort: The idea is, keep comparing before and after two number, if the order is not exchanged, each time in the large number in the back, each cycle will be able to put the largest number out.

Example: 2 5 8 6 1 4

Compare 2 5, normal, not moving

Compare 5 8, normal, not moving

Compare 8 6, no, swap

Compare 8 1, no, swap

Compare 8 4, no, swap

The last 8 will come out, after many cycles can be sorted, it is worth noting that each cycle can be less than a number of loops, because that number has been bubbling out, such as the next time do not have to compare with 8.

Quick sort: The idea is to find a number of signs placed in the first place, and then based on the number of marks from the beginning of the first two, find a greater than the number of flags, a number less than the number of two, and then exchange two numbers, and finally when the meeting, the number of points and the number of symbols exchange position, The left sequence of the comparison flag is then used to do this again, and the right side is the same.

Example: 2 5 8 6 1 4

2 for the number of flags

From the first 4 onwards, we looked forward and found 1:2 small

From the beginning of the first 2, we looked backwards and found 5:2 large

Exchange 5 and 1

The original sequence becomes 2 1 8 6 5 4

From the first 5 onwards, we looked forward and found 1:2 small (meet)

Exchange 1 and 2

The original sequence becomes 1 2 8 6 5 4

And then 2, the left-hand sequence is aligned.

2 The right sequence can be sorted using the previous method

Summary: Bucket sequencing time complexity O (n+m), Bubble sort O (n^2), quick sort O (N logn), in fact I often is a sort () in lieu of all the methods, do not understand the sorting algorithm, so sometimes the convenience will cause the loopholes that do not understand.

Simple three sort

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.