coder • 11个月前
void insertion_sort() {
for (int i = 0; i < n; i++) {
cout << "Insert element[" << i + 1 << "]:" << endl;
cout << "Init:";
for (int j = 0; j <= i; j++) {
cout << a[j] << " ";
}
cout << endl;
int key = a[i], j;
for (j = i - 1; j >= 0 && a[j] > key; j--) {
a[j + 1] = a[j];//后移
cout << "Move back:";
for (int k = 0; k <= i; k++) {
cout << a[k] << " ";
}
cout << endl;
}
a[j + 1] = key;
cout << "Final:";
for (int k = 0; k <= i; k++) {
cout << a[k] << " ";
}
cout << endl;
}
}
评论:
请先登录,才能进行评论