Direct insertion sort (C language version)

Direct insertion sort:

#include<stdio.h>
void swap(int *a,int *b);
int main()
{
	int a[11]={10,4,7,3,9,2,4,6,8,2,3};
	int i,j;
	int length=sizeof(a)/sizeof(a[0]);
	for(i=1;i<length;i++)
	{
		for(j=i-1;j>=0;j--)
		{
			if(a[j]>a[j+1])
				swap(&a[j],&a[j+1]);
		}
	}
	for(i=0;i<length;i++)
		printf("%d ",a[i]);
}
void swap(int *a,int *b)
{
	int temp;
	temp=*a;
	*a=*b;
	*b=temp;
}

 

Published 33 original articles · won praise 30 · views 20000 +

Guess you like

Origin blog.csdn.net/baidu_15547923/article/details/99701597