最直观的插入排序
//
// Created by heolis on 19-5-3.
//
#include <bits/stdc++.h>
using namespace std;
void insertSort(int A[], int n) {
for (int i = 1; i < n; ++i) {
int temp = A[i], j = i; // temp临时存放A[i],j从i开始往前枚举
while (j > 0 && temp < A[j - 1]) { // 只要temp 小于前一个元素A[j-1]
A[j] = A[j - 1]; // 把A[j - 1]后移一位至A[j]
j--;
}
A[j] = temp; // 插入位置为j
}
}
void print(int n) {
printf("%d ", n);
}
int main() {
int num[] = {-1, 12, 5, 9, 8, 5, 6, -8, 2, 1, 3};
int length = sizeof(num) / sizeof(int);
for_each(num, num + length, print);
insertSort(num, length);
printf("\n");
for_each(num, num + length, print);
return 0;
}