-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionsort4.cpp
More file actions
134 lines (116 loc) · 3.1 KB
/
insertionsort4.cpp
File metadata and controls
134 lines (116 loc) · 3.1 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
//#include <utility>
#include <iostream>
#include <vector>
void printArray(int arr[], int size);
void printArrayBar(int A[], int size);
// print bar
void bar(int value);
/*
void insertionSort(int arr[], int size)
{
int i, j, key;
for (i = 1; i < size; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
//printArray(arr, size);
printArrayBar(arr, size);
j = j - 1;
}
arr[j + 1] = key;
//printArray(arr, size);
printArrayBar(arr, size);
}
}
*/
// https://en.wikipedia.org/wiki/Insertion_sort
// Simple implementation: Jon Bentley shows a three-line C/C++ version that is five lines
// when optimized.[1]
/* Simplest insertion sort */
/*
void isort1(int x[], int n)
{
int i, j;
for (i = 1; i < n; i++)
for (j = i; j > 0 && x[j-1] > x[j]; j--) {
//std::swap(j-1, j); // NOTE: there is an error in the Programming Pearls book
std::swap(x[j-1], x[j]);
//printArray(arr, size);
printArrayBar(x, n);
}
}
*/
template <typename T>
void insertion_sort(std::vector<T> &v)
{
for (int i=1; i<v.size(); i++) {
// de i eerste elementen staan reeds in volgorde
T hulp = v[i]; // we halen het i-de element er uit.
int j=i-1;
while (j>=0 && hulp<v[j]) {
// alle elementen groter dan het i-de element worden 1 plaats naar rechts opgeschoven
v[j+1] = v[j];
j--;
printArrayBar(&v[0], v.size());
}
v[j+1] = hulp; // we voegen het uitgehaalde in op zijn juiste plaats.
printArrayBar(&v[0], v.size());
}
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
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);
}
int main()
{
//int arr[] = { 43, 27, 100, 10, 67, 1, 90, 45, 87, 78, 74, 65, 13, 5, 77, 33 };
//int size = sizeof(arr) / sizeof(arr[0]);
std::vector<int> vec{ 43, 27, 100, 10, 67, 1, 90, 45, 87, 78, 74, 65, 13, 5, 77, 33 };
//insertionSort(arr, size);
//isort1(arr, size);
insertion_sort(vec);
//printArray(arr, size);
for (auto i: vec)
std::cout << i << ' ';
std::cout << std::endl;
}