-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithms.js
177 lines (156 loc) · 3.94 KB
/
algorithms.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
function* bubbleSort(arr) {
var swapped = false;
console.log("in");
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
//comapre return arr[j]>arr[j+1]
if (compare(arr, j, j + 1)) {
swap(arr, j, j + 1);
swapped = true;
}
yield;
}
}
if (swapped == false) {
return;
}
}
function* selectionSort(arr) {
for (let i = 0; i < arr.length - 1; i++) {
let minIdx = i;
for (let j = i + 1; j < arr.length; j++) {
if (compare(arr, minIdx, j)) {
minIdx = j;
}
yield;
}
swap(arr, i, minIdx);
yield;
}
}
function* mergeSort(arr) {
yield* _mergeSort(arr, 0, arr.length - 1);
}
function* _mergeSort(arr, l, r) {
if (l >= r) return;
const m = l + Math.floor((r - l) / 2);
yield* _mergeSort(arr, l, m);
yield* _mergeSort(arr, m + 1, r);
yield* _merge(arr, l, m, r);
}
function* _merge(arr, l, m, r) {
const n1 = m - l + 1;
const n2 = r - m;
let L = new Array(n1);
let R = new Array(n2);
for (let i = 0; i < n1; i++) L[i] = arr[l + i];
for (let j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
let i = 0;
let j = 0;
let k = l;
while (i < n1 && j < n2) {
find_and_compare(L[i].val, R[j].val);
if (L[i].val < R[j].val) {
find_and_swap(arr[k].val, L[i].val);
arr[k] = L[i];
k++;
i++;
}
//increment after the value given
else {
find_and_swap(arr[k].val, R[j].val);
arr[k] = R[j];
k++;
j++;
}
yield;
}
while (i < n1) {
find_and_swap(arr[k].val, L[i].val);
arr[k++] = L[i++];
yield;
}
while (j < n2) {
find_and_swap(arr[k].val, R[j].val);
arr[k++] = R[j++];
yield;
}
}
function* quickSortLomuto(arr) {
//tricky to understand
yield* _quickSortLomuto(arr, 0, arr.length - 1);
}
function* _quickSortLomuto(arr, left, right) {
if (left >= right) {
return;
}
let partitionGenerator = _partitionLomuto(arr, left, right);
let result = partitionGenerator.next();
//we need the pivot so wee run this till we the poviot
while (!result.done) {
result = partitionGenerator.next();
yield 1;
}
let idx = result.value;
yield* _quickSortLomuto(arr, left, idx - 1);
yield* _quickSortLomuto(arr, idx + 1, right);
}
function* _partitionLomuto(arr, left, right) {
let pivot = arr[right].val;
let i = left - 1;
for (let j = left; j < right; j++) {
arr[right].pivot = true;
if (arr[j].val < pivot) {
compare(arr, j, right);
swap(arr, ++i, j);
yield;
}
}
swap(arr, i + 1, right);
yield;
return i + 1;
}
function swap(arr, x, y) {
//set color and swap
arr[x].swap = true;
arr[y].swap = true;
temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
function compare(arr, x, y) {
//set color and compare
arr[x].compare = true;
arr[y].compare = true;
return arr[x].val > arr[y].val;
}
function find_and_compare(val1, val2) {
//find the loc of val in arr and set the color
index_1 = 0;
index_2 = 0;
for (var i = 0; i < arr.length; i++) {
if (arr[i].val == val1) {
index_1 = i;
}
if (arr[i].val == val2) {
index_2 = i;
}
}
arr[index_1].compare = true;
arr[index_2].compare = true;
}
function find_and_swap(val1, val2) {
//find the loc of val in arr and set the color
index_1 = 0;
index_2 = 0;
for (var i = 0; i < arr.length; i++) {
if (arr[i].val == val1) {
index_1 = i;
}
if (arr[i].val == val2) {
index_2 = i;
}
}
arr[index_1].swap = true;
arr[index_2].swap = true;
}