void InsertSort(vector<int> &a){
int n = a.size();
int j = 0;
int i = 1;
int x = 1;
while (x < n){
int temp = a[i];
if (temp >= a[j]){//如果當前數字比前面已經排好的數組最大的(也就是最後一個)還大,那就繼續往後找
i++;
j++;
x++;
continue;
}
else{
while (j >= 0 && temp < a[j]){//如果當前數字比前面已經排好的數組最大的(也就是最後一個)小
a[i] = a[j];
i--;
j--;
}
a[i] = temp;
x++;
i = x;
j = i - 1;
}
}
}
思想是,假定前面是已經排過的數,當前數要插在合適位置,