// Note:Your choice is C++ IDE
#include <iostream>
using namespace std;
void InsertSort(int A[],int n){
int i,j,high,low,mid,temp;
for(i=1;i<n;i++){ //依次将A[2]之后的数据插入前面已经排序好的数组中
temp=A[i]; //暂放在第一位
low=1;high=n-1;
while(low<=high){
mid=(low+high)/2; //mid为中点
if(A[mid]>temp)
high=mid-1;
else low = mid+1;
}
for(j=i-1;j>=low;j--)
{A[j+1]=A[j];} //往后移
A[low] = temp;
}
}
void PrintA(int A[],int n){
for(int i=0;i<n;i++){
cout<<A[i]<<" ";
}
cout<<endl;
}
int main()
{
int A[] = {22,11,33,66,88,99,77,44,55};
int n = sizeof(A)/sizeof(A[0]);
PrintA(A,n);
InsertSort(A,n);
PrintA(A,n);
return 0;
}
运行截图: