"Algorithm" Insert sort C language implementation

Source: Internet
Author: User

Do you like to play poker? Haha, I like it, especially the three landlords, like. Now let me describe a picture to see if we are familiar.


My habit of grasping cards is, in the grasp of the card, I want to look at my cards, look at the status of the card, there is no big kid , there are several 2, there is no long even, by the way to do a basic sort of work . For example, my first card grabbed 7, Put in the hand, the second card is J, I put it in the back of 7 (yes, I default to the left to right ascending ), the third is ten, I put it between 7 and J , the fourth card or Ten, then I have two positions to prevent this just arrived, one is to put him in front of J , two is to put it in front of the first ten is 7 , both of these methods are desirable (Virgo is more than the color and then choose to place the location, you can not hurt AH), and so on, until you catch all the cards,OK, call the landlord, rob the landlord, open dry.


The bedroom is full of picture.


Some people will ask at this time, you do not want to talk about the insertion of the sort, how to drag the landlord up? For such classmates, I can only say that this question is very good. Please let me on my knees to solve your doubts.


Do not know if we have found that we have been in the grasp of the card is always doing the insertion and comparison (the size of the comparison card) operation, in fact, we are going to talk about the insertion sort and grasp the principle of almost exactly the same, the reason first with everyone is a moment landlord, is to help you warm up a bit, So that you can better understand the principle of implementation of the code and the idea between.


OK, so much to say, or the code:


void Insert_sort (int array[], int length) {int I;int j;int temp;//Here temp is as Sentinel, monitor Array[i] value for (i=1; i<length; i++) {if (Array[i] < array[i-1]) {//At this time, the sequence before Array[i] is already the value of the ordered state temp = array[i];//deposit Array[i] for (j=i-1; array[j]>temp; j--) {//Find the first one less than temp is array[i] The value of the loop ends Array[j + 1] = array[j];//will array[j] move back one position}array[j + 1] = temp;//The value of the Sentinel store's array[i] is filled in to the last remaining correct position. This makes array[0]~array[i] an orderly}}



After reading the code and comments or do not understand, please calm down. Imagine that our first element at the beginning of a sequence is a sequence, because it has only one element, so it must be orderly. So we'll take his next element and combine it into a sequence of two elements, Then compare their size, if the latter element [later called it is rear] than the previous element [then called it is front] small, then the first to save rear to the Sentinel here, and then front back to move one, covering the original front, At this time we don't have to worry about the value of front will be lost, because we have already stored the value of rear in Sentinel temp (now see the role of Sentinel!), Finally, the Sentinel's value is passed on to front. So that they can form an orderly queue. After that, we continue to iterate and compare the swap position until the end.


In the direct insertion sort, the Sentinel's use is very ingenious, needs everybody to ponder carefully, absorbs this thought, in the program which you write later to apply to that is extremely good. But this is not to say to do, still need to rely on your code amount, mutual encouragement, thank you.

"Algorithm" Insert sort C language implementation

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.