插入排序基本原理
#include <iostream>
using namespace std;
int main(){
//插入排序基本原理
int a[] = { 9,8,1,4,2,3,0,6,7,5 };
int n=sizeof(a)/sizeof(a[0]);
for (int j = 0; j < n; j++) {
cout << a[j] << " ";
}
cout << endl;
int value=a[1];
int index=1-1;
while(index>=0 && a[index]>value){
a[index+1]=a[index];
index--;
}
a[index+1]=value;
for (int j = 0; j < n; j++) {
cout << a[j] << " ";
}
cout << endl;
value=a[2];
index=2-1;
while(index>=0 && a[index]>value){
a[index+1]=a[index];
index--;
}
a[index+1]=value;
for (int j = 0; j < n; j++) {
cout << a[j] << " ";
}
cout << endl;
value=a[3];
index=3-1;
while(index>=0 && a[index]>value){
a[index+1]=a[index];
index--;
}
a[index+1]=value;
for (int j = 0; j < n; j++) {
cout << a[j] << " ";
}
cout << endl;
value=a[4];
index=4-1;
while(index>=0 && a[index]>value){
a[index+1]=a[index];
index--;
}
a[index+1]=value;
for (int j = 0; j < n; j++) {
cout << a[j] << " ";
}
cout << endl;
value=a[5];
index=5-1;
while(index>=0 && a[index]>value){
a[index+1]=a[index];
index--;
}
a[index+1]=value;
for (int j = 0; j < n; j++) {
cout << a[j] << " ";
}
cout << endl;
value=a[6];
index=6-1;
while(index>=0 && a[index]>value){
a[index+1]=a[index];
index--;
}
a[index+1]=value;
for (int j = 0; j < n; j++) {
cout << a[j] << " ";
}
cout << endl;
value=a[7];
index=7-1;
while(index>=0 && a[index]>value){
a[index+1]=a[index];
index--;
}
a[index+1]=value;
for (int j = 0; j < n; j++) {
cout << a[j] << " ";
}
cout << endl;
value=a[8];
index=8-1;
while(index>=0 && a[index]>value){
a[index+1]=a[index];
index--;
}
a[index+1]=value;
for (int j = 0; j < n; j++) {
cout << a[j] << " ";
}
cout << endl;
value=a[9];
index=9-1;
while(index>=0 && a[index]>value){
a[index+1]=a[index];
index--;
}
a[index+1]=value;
for (int j = 0; j < n; j++) {
cout << a[j] << " ";
}
cout << endl;
getchar();
return 0;
}