-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSortCpp.cpp
More file actions
38 lines (31 loc) · 780 Bytes
/
InsertionSortCpp.cpp
File metadata and controls
38 lines (31 loc) · 780 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
#include <iostream>
using namespace std;
template <class T>
void Print(T& vec, int n, string s) {
cout << s << ": [" << flush;
for (int i = 0; i < n; i++) {
cout << vec[i] << flush;
if (i < n - 1) {
cout << ", " << flush;
}
}
cout << "]" << endl;
}
void InsertionSort(int A[], int n) {
for (int i = 1; i < n; i++) {
int j = i - 1;
int x = A[i];
while (j > -1 && A[j] > x) {
A[j + 1] = A[j];
j--;
}
A[j + 1] = x;
}
}
int main() {
int A[] = { 19, 17, 15, 13, 11, 9, 7, 5, 3, 1 };
Print(A, sizeof(A) / sizeof(A[0]), " A");
InsertionSort(A, sizeof(A) / sizeof(A[0]));
Print(A, sizeof(A) / sizeof(A[0]), "Sorted A");
return 0;
}