Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created October 9, 2019 17:18
Show Gist options
  • Save surinoel/7eb44794bd6c667d1201f0581eace56e to your computer and use it in GitHub Desktop.
Save surinoel/7eb44794bd6c667d1201f0581eace56e to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
using namespace std;
void swap(int* a, int* b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 1; i < n; i++) {
int j = i - 1;
while (j >= 0 && a[j] > a[j + 1]) {
swap(&a[j], &a[j + 1]);
j--;
}
}
for (int i = 0; i < n; i++) {
cout << a[i] << ' ';
}
cout << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment