/*C++实现插入排序*/ #include <iostream> using namespace std; void insertsort(int a[],int length) { int j=1; for(;j<length;j++) { int key=a[j]; int i=j-1; while(i>=0&&a[i]>key) { a[i+1]=a[i]; i--; } a[i+1]=key; } return ; } int main() { int a[20]={12,5,78,45,1,3,26,14,12,45,14,54,78,123,4,0,78,78,6,9}; int length=sizeof(a)/sizeof(int); cout<<"排序前:"; int i=0; for(;i<length;i++) { cout<<a[i]<<" "; } cout<<endl; insertsort(a,length); cout<<"排序后:"; i=0; for(;i<length;i++) { cout<<a[i]<<" "; } cout<<endl; cout<<"sizeof(a): "<<sizeof(a)<<endl; return 0; }