Skip to content

Commit eaeca25

Browse files
authored
Create Insertion Sort
1 parent 86d0470 commit eaeca25

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

insertion_sort.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include<bits/stdc++.h>
2+
#include<string>
3+
using namespace std;
4+
#define int long long
5+
6+
// bool compareQ(pair<int, int> a, pair<int, int> b)
7+
// {
8+
// if (a.first < b.first)
9+
// return (a.second > b.second);
10+
11+
12+
// return false;
13+
// // }
14+
// int N = 1e9 + 2;
15+
void insertion_sort(int a[], int n)
16+
{
17+
for (int i = 0; i < n; i++)
18+
{
19+
int key = a[i];
20+
int j = i - 1;
21+
while (j >= 0 && a[j] > key)
22+
{
23+
a[j + 1] = a[j];
24+
j--;
25+
}
26+
a[j + 1] = key;
27+
28+
}
29+
}
30+
31+
void solve()
32+
{
33+
34+
int n;
35+
cin >> n;
36+
int a[n];
37+
for (int i = 0; i < n; i++)
38+
{
39+
cin >> a[i];
40+
}
41+
insertion_sort(a, n);
42+
43+
for (int i = 0; i < n; i++)
44+
{
45+
cout << a[i] << " ";
46+
}
47+
}
48+
49+
50+
51+
52+
int32_t main()
53+
{
54+
ios_base::sync_with_stdio(false);
55+
cin.tie(NULL);
56+
57+
#ifndef ONLINE_JUDGE
58+
freopen("input.txt", "r", stdin);
59+
freopen("error.txt", "w", stderr);
60+
freopen("output.txt", "w", stdout);
61+
#endif
62+
63+
long long t = 1;
64+
65+
// cin >> t;
66+
while (t--)
67+
{
68+
69+
solve();
70+
}
71+
72+
73+
74+
}
75+
76+
77+
78+
79+
80+

0 commit comments

Comments
 (0)