Sort overview (C language)

Source: Internet
Author: User

Sorting problems can be said to be the beginning of the introduction of the algorithm, but still, the algorithm this thing is still a lot of beginners to break the brains (such as me~). But, however, the sort summary came (based on c), hoping to give the reader a new experience of understanding, including the author itself.

1. Bubble sort.

The so-called bubble sort, from the literal can generally understand, to a series of numbers, the number of satisfied conditions, let it in turn "bubble surfaced." The basic idea is: each time the adjacent two numbers to compare, will be small number (or large number, to see if you need to reverse or order) to the front. So, if there are 6 numbers, we first retrieve these six numbers, find the smallest place in the first position, and then the next retrieval, just retrieve the remaining five numbers, find the sub-small place in the second position. Follow this idea and proceed in turn.

From the above analysis we can see: if there are n number to be compared, we have to carry on the n-1 comparison, in the first trip to do n-1 22 comparison, the J trip to N-j times to compare.

In the code, that is: (I take 10 numbers for example)

  

The point is, here I write the sequence of steps in a sub-function, with the main function to call this function.

2. Select Sort.

As you can see, bubbling has been superior to the simplest comparison size of if (a>b), but, that is, the system still has to do a large amount of computation. Is there a quicker way? Selection method.

The basic idea of selection method is that the small number of I is placed directly in the position of I, and the order is directly realized.

The C language code is as follows:

  

When dealing with a lot of data, the selection method is significantly faster than bubbling. His time complexity was O (LOGN).

3. Two points to find. (This method is more used to check whether there is a number in a column number, if not, insert this number into the sequence, so that the sequence is still in order to meet the size sequence.) )

Two-point lookup is also called insert sort.

The two sides start, fancy the number of positions, check whether the number is the number you are looking for. If not, then split, then find. Go down in turn. Until: The mark of the number of the first position is greater than the mark of the number of the last position.

If not, insert the new number, as long as the number after the number of the whole to the right to move one position, the vacated position filled in this number.

Look at the code: (I first initialize an array, there is output Yes, no then insert this number and output a new array)

three methods were introduced in one breath, and the gods were asked to guide twos. Personally feel that there is this policy: if you do not understand for a while, may wish to recite a sorting method (I write the sub-function on the line), with the use of, will steady, enlightened ~ ~ ~ ~

Sort overview (C language)

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.