# -Insertion sort ###### tags: `Easy` 數組可分為[已排序]跟[未排序] Insertion: 一直將未排序的插入已排序的 插入實作:通過比較大小作調換(swap),直到要插入的點 ```cpp= #include <vector> using namespace std; vector<int> insertionSort(vector<int> array) { // Write your code here. if (array.empty()) return {}; for (int i = 0; i < array.size(); i++){ int j = i; while(j>0 && array[j]<array[j-1]){ swap(array[j], array[j-1]); j -= 1; } } return array; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up