-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.cpp
More file actions
53 lines (36 loc) · 982 Bytes
/
Copy pathInsertionSort.cpp
File metadata and controls
53 lines (36 loc) · 982 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
45
46
47
48
49
50
51
52
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define no cout << "NO\n"
#define yes cout << "YES\n"
#define endl cout << '\n'
#define vint vector<int>
#define vll vector<ll>
#define pb push_back
#define S second
#define F first
#define wl int t; cin >> t; while(t--)
#define sort(v) sort(v.begin(),v.end())
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
void insrtionsort(vint &v, int n);
int main() {
vint v = {20, 5, 3, 1, 9, 4};
cout << "Before insertion sort :\n";
for(auto &i: v) cout << i << ' '; endl;
insrtionsort(v, 6);
cout << "After insertion sort :\n";
for(auto &i: v) cout << i << ' '; endl;
}
/* the complexity of insertion sort is : O(n²) */
void insrtionsort(vint &v, int n){
int key, z;
for(int i = 1 ; i < n; i++){
key = v[i];
z = i - 1;
while (z >= 0 && v[z] > key){
v[z+1] = v[z];
z--;
}
v[z+1] = key;
}
}