简单插入排序

#include<iostream>
using namespace std;
//定义一个交换函数
void swap(int &a,int &b)
{
	int tmp;
	tmp=a;
	a=b;
	b=tmp;
}
void InsertSort(int *A,int length)
{ 
	

	int i,j;
	for(int i=1;i<length;++i)
	{   
		if(A[i]<A[i-1])
		{
			int j=i;
			while(A[j]<A[j-1]&&j>=1)
			{
				swap(A[j],A[j-1]);
				j--;
			}
		}
	}
}
int main()
{   int a[9]={9,1,5,8,3,7,4,6,2};
InsertSort(a,9);
    
		for(int i=0;i<9;++i)
			cout<<a[i]<<" ";
		cout<<endl;
	system("pause");
	return 0;
}

原文地址:https://www.cnblogs.com/mfrbuaa/p/5223451.html