-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.cpp
More file actions
44 lines (37 loc) · 782 Bytes
/
Copy pathinsertion_sort.cpp
File metadata and controls
44 lines (37 loc) · 782 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
/*
* 2019.1.7
* insertion_sort
*/
#include <iostream>
using namespace std;
void insertionSort(int *arr, int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j >= 0; j--) {
if (arr[j - 1] > arr[j]) {
int temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
}
}
void show(int *arr, int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int N, input;
cin >> N;
int *arr = new int[N];
for (int i = 0; i < N; i++) {
cin >> input;
arr[i] = input;
}
insertionSort(arr, N);
show(arr, N);
system("pause");
return 0;
}
// 10 5 6 3 8 4 9 7 2 1 10