-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.cpp
More file actions
executable file
·36 lines (35 loc) · 1.05 KB
/
Copy pathinsertion_sort.cpp
File metadata and controls
executable file
·36 lines (35 loc) · 1.05 KB
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
// This is my implementation of insertion sort.
// I believe I was trying to roughly measure the complexity
// of it by counting how many times std::swap got called.
// The file properties says it was last modified on 12.1.2019.
// At the time of writing this comment, it is now 17.5.2019.
#include <iostream>
#include <vector>
#include <random>
#include <numeric>
#include <algorithm>
int insertion_sort(std::vector<int> & vec) {
int swapCount = 0;
for (int i = 0; i < vec.size(); ++i) {
for (int j = i; j > 0 && vec[j] < vec[j-1]; --j) {
std::swap(vec[j-1], vec[j]);
++swapCount;
}
}
return swapCount;
}
int main() {
std::vector<int> vec(100);
std::iota(vec.begin(), vec.end(), 0);
std::shuffle(vec.begin(), vec.end());
for (int i : vec) {
std::cout << i << ' ';
}
std::cout << "\n-------------------------------\n";
int swapCount = insertion_sort(vec);
for (int i : vec) {
std::cout << i << ' ';
}
std::cout << "\nswap count = " << swapCount << std::endl;;
return 0;
}