插入排序

· · 个人记录

#include <iostream>
#include <cmath>

using namespace std;

int n, sum = 1;
int a[1000];

int main() {
    cin >> n;
    for (int i = 0; i < n; cin >> a[i++]);
    for (int i = 1, j, no; i < n; i++) {
        for (j = i - 1, no = a[i]; j >= 0; j--) {
            if (a[j] > no) {
                a[j + 1] = a[j];
                cout << sum++ << ".  ";
                for (int k = 0; k < n; k++) {
                    cout << a[k] << " ";
                }
                cout << endl << endl;
            }
            else {
                break;
            }
        }
        a[j + 1] = no;
    }
    cout << "最终:  ";
    for (int k = 0; k < n; k++) {
        cout << a[k] << " ";
    }
    return 0;
}