-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sorts.cpp
76 lines (72 loc) · 2.26 KB
/
merge_sorts.cpp
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
#include "vizsorting.hpp"
void App::merge(int a[], int aux[], int l, int m, int r) {
for (int i = l, j = m, k = l; k < r; k++) {
if (i == m) { a[k] = aux[j++]; continue; }
if (j == r) { a[k] = aux[i++]; continue; }
a[k] = (aux[j] > aux[i]) ? aux[i++]:aux[j++];
lcv = i;
rcv = j;
render();
}
}
void App::mergesort(int a[], int aux[], int l, int r) {
if (r - l < 13) {
insertionsort(a, l, r+1);
return;
}
int m = (l+r)/2;
mergesort(aux, a, l, m);
mergesort(aux, a, m, r);
merge(a, aux, l, m, r);
}
void App::mergesort(int a[], int n) {
int *aux = new int[n];
for (int i = 0; i < n; i++)
aux[i] = a[i];
mergesort(a, aux, 0, n);
lcv = rcv = n + 1;
}
void App::bumsort(int a[], int n) {
int *aux = new int[n];
for (int w = 1; w < n; w *= 2) {
for (int b = 0; b < n-w; b += 2*w) {
int l = b, m = l+w, r = min(l+w+w, n);
for (int i = 0; i < n; i++)
aux[i] = a[i];
for (int k = l, i = l, j = m; k < r; k++) {
if (i == m) { a[k] = aux[j++]; continue; }
if (j == r) { a[k] = aux[i++]; continue; }
a[k] = (aux[j] > aux[i]) ? aux[i++]:aux[j++];
lcv = i;
rcv = j;
render();
}
}
}
lcv = rcv = n + 1;
}
//This is _not_ Timsort. I can't believe that needs to be said.
void App::tiledbumsort(int a[], int n) {
for (int i = 0; i < n; i += 9) {
insertionsort(a, i, i+9);
}
int *aux = new int[n];
for (int i = 0; i < n; i++)
aux[i] = a[i];
for (int w = 9; w < n; w = 2*w) {
for (int b = 0; b < n-w; b = b + 2*w) {
int l = b, m = l+w, r = min(l+w+w, n);
for (int i = 0; i < n; i++)
aux[i] = a[i];
for (int k = l, i = l, j = m; k < r; k++) {
if (i == m) { a[k] = aux[j++]; continue; }
if (j == r) { a[k] = aux[i++]; continue; }
a[k] = (aux[j] > aux[i]) ? aux[i++]:aux[j++];
lcv = i;
rcv = j;
render();
}
}
}
lcv = rcv = n + 1;
}