插入排序

来源:互联网 发布:混也是一种生活 知乎 编辑:程序博客网 时间:2024/06/05 19:06


void print(int a[], int n, int i){
cout << "第" << i + 1 << "趟 : ";
for (int j = 0; j < n; j++){
cout << a[j] << "   ";
}
cout << endl;
}




/**
* 插入排序
*/
void InsertSort(int a[], int n){


/*
初始值:3  1  5  7  2  4  9  6
*/
for (int i = 1; i < n; i++){
if (a[i] < a[i - 1]){
int j = i - 1;
int x = a[i];
a[i] = a[j];
while (x < a[j]){
a[j + 1] = a[j];
j--;
}
a[j + 1] = x;
}
print(a, n, i);
}
}
0 0