Insert sort directly

Source: Internet
Author: User

The basic idea of insert sorting is that the forward side of the array is sorted, and the next element is located in the front and inserted. For example, you have sorted the order in the middle 1 2 4 7, and then locate the position 2 and insert it between 1 and 3. And then process 4 and 9.

The Reference Program (implemented in C) is as follows:

# Include <stdio. h>
Void insection_sort (int * a, int array_size)
{
Int I, J, K, TMP;
For (I = 1; I <array_size; I ++)
{
TMP = A [I];
J = I-1;
While (j> = 0 & A [J]> TMP)
{
A [J + 1] = A [J];
J --;
}
A [J + 1] = TMP;
}
}

Int main ()
{
Int A [7] = {4, 7, 1, 3}, I;
Insection_sort (A, 4 );
For (I = 0; I <7; I ++)
{
Printf ("% d", a [I]);
}
Printf ("\ n ");
Return 0;
}

The Execution Process in the sorting process is as follows:

 

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.