-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionsort2.cpp
More file actions
74 lines (60 loc) · 1.71 KB
/
insertionsort2.cpp
File metadata and controls
74 lines (60 loc) · 1.71 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
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
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
#include <unistd.h>
#include <sys/ioctl.h>
// print bar
void bar(int value);
void printArrayBar(int A[], int size)
{
system("clear");
for (int i = 0; i < size; i++) {
bar(A[i]);
printf("\n");
}
usleep(200000);
}
void bar (int value) {
// Print progress bar
//system("clear");
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
int barWidth = w.ws_col - 10;
float progress = 0.0;
//while (progress < 1.0) {
while (progress < value/100.0) {
//printf("\r%3d%% ", (int)(progress * 100.0));
printf("\r%3d ", value);
int pos = barWidth * progress;
for (int i = 0; i < barWidth; i++) {
if (i <= pos) printf("\u2588");
else printf(" ");
}
fflush(stdout);
progress += 0.02; // test
//usleep(100000);
}
//printf("\r100%%\n");
//printf("\r%i", value);
}
template <typename T>
void insertion_sort(std::vector<T> &v)
{
for (auto i = v.begin(); i != v.end(); ++i) {
std::rotate(std::upper_bound(v.begin(), i, *i), i, std::next(i));
// NOTE: lijkt snel maar dat komt omdat we niet elke stap kunnen printen
// rotate() zet de waarde in een keer op de juiste plek
// En kun je dit wel een insertion sort noemen? Eerder rotation sort.
for (auto i: v)
std::cout << i << ' ';
std::cout << std::endl;
//printArrayBar(&v[0], v.size());
}
}
int main()
{
std::vector<int> vec{ 43, 27, 100, 10, 67, 1, 90, 45, 87, 78, 74, 65, 13, 5, 77, 33 };
insertion_sort(vec);
for (auto i: vec)
std::cout << i << ' ';
std::cout << std::endl;
}