-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathE0092.cpp
More file actions
41 lines (36 loc) · 726 Bytes
/
E0092.cpp
File metadata and controls
41 lines (36 loc) · 726 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
/*
Problem Statement: https://www.hackerrank.com/challenges/insertionsort1/problem
*/
#include <iostream>
#include <vector>
using namespace std;
void printArray(int n, vector<int> arr) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
void insertionSort1(int n, vector<int> arr) {
bool flag;
int key;
for (int j, i = 1; i < n; i++) {
key = arr[i];
flag = (arr[i] < arr[i - 1]) ? true : false;
for (j = i; j > 0 && key < arr[j - 1]; j--) {
arr[j] = arr[j - 1];
printArray(n, arr);
}
if (flag) {
arr[j] = key;
printArray(n, arr);
}
}
}
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++)
cin >> arr[i];
insertionSort1(n, arr);
return 0;
}