Skip to content

Instantly share code, notes, and snippets.

@aximov
Created May 13, 2020 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aximov/ce6577e5973226fa3cff4ae8b15649e3 to your computer and use it in GitHub Desktop.
Save aximov/ce6577e5973226fa3cff4ae8b15649e3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
void printVector(vector<int> A) {
for (int i = 0; i < A.size() - 1; ++i) {
cout << A[i] << " ";
}
cout << A.back() << endl;
}
vector<int> insertionSort(vector<int> A) {
for (int i = 1; i < A.size(); ++i) {
int v = A[i];
int j = i - 1;
while (j >= 0 && A[j] > v) {
A[j + 1] = A[j];
--j;
}
A[j + 1] = v;
printVector(A);
}
return A;
}
int main() {
int N;
cin >> N;
vector<int> A;
int a;
while(cin >> a) {
A.push_back(a);
}
printVector(A);
insertionSort(A);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment