算法:
void InsertSort(char array[],unsigned int n)
{ int i,j; int temp; for(i=1;i<n;i++) { temp = array;//store the original sorted array in temp for(j=i ; j>0 && temp < array[j-1] ; j--)//compare the new array with temp { array[j]=array[j-1];//all larger elements are moved one pot to the right } array[j]=temp; }}
//简单具体实例
// My.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void InsertSort(int array[],int n)
{
int i,j;
int temp;
for(i=1;i<n;i++)
{
temp=array[i];
for(j=i;j>0&& temp<array[j-1];j--)
{
array[j]=array[j-1];
}
array[j]=temp;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int i;
int n;
cout<<"请输入您要排序的数组元素个数:";
cin>>n;
//创建数组
int *array;
array=new int [n];
cout<<"\n请输入数组元素:"<<endl;
for(i=0;i<n;i++)
{
cin>>array[i];
}
//排序
InsertSort(array,n);
//输出
for(i=0;i<n;i++)
{
cout<<array[i]<<" ";
}
return 0;
}