Direct Insert Sort C language implementation

Source: Internet
Author: User

Direct insertion Sorting is the simplest sort algorithm, and the basic idea is that each time a sorted record is inserted into the previously sorted subsequence by its keyword size until all the data is complete.

Its corresponding C code is implemented as follows:

#include "stdio.h"

void Insertsort (int a[], int n)//Direct Insert Sort
{
int i,j,temp=0;
for (i=1;i<n;i++)
{
if (A[i]<a[i-1])
{
temp = A[i];
For (j=i-1;j>=0 && a[j]>temp;j--)
{
A[J+1]=A[J];
}
A[j+1]=temp; //
}
}

}

void Main ()
{
int a[10]={0,6,67,34,56,45,12,4,7,49};
int i=0;
Insertsort (a,10);
for (i=0;i<10;i++)
printf ("%d", a[i]);

}

The time complexity of the algorithm is O (n^2), the space-assist degree is O (1), because the sorting algorithm compares the record size first, so it is a stable sorting algorithm.

Direct 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.