#include<iostream>
using namespace std;
void print(int *arr,int length)
{
for(int i = 0;i <
length;i++)
{
cout<<arr[i]<<"\t";
}
cout<<"\n";
}
void sort(int *arr,int length)
{
int temp = 0;
int times
= 0;
for(int i = 0;i < length-1;i++)
{
temp
= arr[i];
times = i-1;
while(times >= 0
&& temp <
arr[times])
{
arr[times+1] =
arr[times];
times--;
}
arr[++times]
= temp;
}
}
int main()
{
int arr[] = {8,5,7,4,6,2,3,1,9};
int size =
sizeof(arr)/sizeof(int);
cout<<"排序前数组元素为:"<<endl;
print(arr,size);
sort(arr,size);
cout<<"排序后数组元素为:"<<endl;
print(arr,size);
return
0;
}
原文:http://www.cnblogs.com/WangYinlong/p/3549075.html