-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion_Sort.cpp
More file actions
44 lines (41 loc) · 923 Bytes
/
Copy pathInsertion_Sort.cpp
File metadata and controls
44 lines (41 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define sz(x) (int)(x).size()
#define rd ({ ll x; cin >> x; x; })
vector<ll> insertion_sort(ll n, vector<ll>& a) {
for (ll i = 1; i <= n; ++i) {
ll key = a[i];
ll j = i - 1;
while (j >= 0 && a[j] > key) {
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = key;
}
return a;
}
void solve() {
ll n;
cin >> n;
vector<ll> a(n + 1);
for (ll i = 1; i <= n; ++i) {
cin >> a[i];
}
vector<ll> sorted_array = insertion_sort(n, a);
for (ll i = 1; i <= n; ++i) {
cout << sorted_array[i] << " ";
}
cout << '\n';
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
//int t = 1;
//cin >> t;
//for (int i = 1; i <= t; i++)
solve();
return 0;
}