-
Notifications
You must be signed in to change notification settings - Fork 6
/
insertsion sort.cpp
64 lines (52 loc) · 1.31 KB
/
insertsion sort.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
#include<bits/stdc++.h>
#define PI acos(-1)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define lli long long int
#define ull unsigned long long int
#define pii pair < int, int>
#define pll pair < ll, ll>
#define sc scanf
#define scin(x) sc("%d",&(x))
#define scln(x) sc("%lld",&(x))
#define pf printf
#define ms(a,b) memset(a,b,sizeof(a))
#define veci vector<int>
#define vecl vector<long long int>
#define pb push_back
using namespace std;
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int n,i;
cin >> n ;
int arr[n];
for(i=0;i<n;i++)
cin >> arr[i] ;
insertionSort(arr, n);
printArray(arr, n);
return 0;
}