Skip to content

Commit a2aa805

Browse files
author
Oskar Widmark
committed
fix: only pass drawIteration when parallel is true
1 parent e02bac7 commit a2aa805

File tree

1 file changed

+101
-17
lines changed

1 file changed

+101
-17
lines changed

src/sorting-algorithms.ts

Lines changed: 101 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,41 @@ export class SortingAlgorithms {
9898
const oddSorter = async () => {
9999
for (let i = 1; i < arr.length; i += 2) {
100100
if (
101-
await this.context.compare(arr, i - 1, '>', i, drawIteration + 0.5)
101+
await this.context.compare(
102+
arr,
103+
i - 1,
104+
'>',
105+
i,
106+
options.parallel ? drawIteration + 0.5 : undefined,
107+
)
102108
) {
103-
await this.context.drawAndSwap(arr, i - 1, i, drawIteration);
109+
await this.context.drawAndSwap(
110+
arr,
111+
i - 1,
112+
i,
113+
options.parallel ? drawIteration : undefined,
114+
);
104115
isSorted = false;
105116
}
106117
}
107118
};
108119
const evenSorter = async () => {
109120
for (let i = 2; i < arr.length; i += 2) {
110121
if (
111-
await this.context.compare(arr, i - 1, '>', i, drawIteration + 0.5)
122+
await this.context.compare(
123+
arr,
124+
i - 1,
125+
'>',
126+
i,
127+
options.parallel ? drawIteration + 0.5 : undefined,
128+
)
112129
) {
113-
await this.context.drawAndSwap(arr, i - 1, i, drawIteration);
130+
await this.context.drawAndSwap(
131+
arr,
132+
i - 1,
133+
i,
134+
options.parallel ? drawIteration : undefined,
135+
);
114136
isSorted = false;
115137
}
116138
}
@@ -156,7 +178,7 @@ export class SortingAlgorithms {
156178
index1,
157179
'>',
158180
index2,
159-
drawIteration + 0.5,
181+
options.parallel ? drawIteration + 0.5 : undefined,
160182
)
161183
) {
162184
await this.context.drawAndSwap(
@@ -217,10 +239,15 @@ export class SortingAlgorithms {
217239
start,
218240
'>',
219241
start + dist,
220-
drawIteration + 0.5,
242+
options.parallel ? drawIteration + 0.5 : undefined,
221243
)
222244
) {
223-
await this.context.drawAndSwap(arr, start, start + dist, drawIteration);
245+
await this.context.drawAndSwap(
246+
arr,
247+
start,
248+
start + dist,
249+
options.parallel ? drawIteration : undefined,
250+
);
224251
}
225252
return;
226253
}
@@ -247,8 +274,21 @@ export class SortingAlgorithms {
247274
for (let i = start + dist; i < end - dist; i += newDist) {
248275
fns.push(async () => {
249276
const j = i + dist;
250-
if (await this.context.compare(arr, i, '>', j, drawIteration + 0.5)) {
251-
await this.context.drawAndSwap(arr, i, j, drawIteration);
277+
if (
278+
await this.context.compare(
279+
arr,
280+
i,
281+
'>',
282+
j,
283+
options.parallel ? drawIteration + 0.5 : undefined,
284+
)
285+
) {
286+
await this.context.drawAndSwap(
287+
arr,
288+
i,
289+
j,
290+
options.parallel ? drawIteration : undefined,
291+
);
252292
}
253293
});
254294
}
@@ -440,16 +480,38 @@ export class SortingAlgorithms {
440480
// i & k is false for the first half of the bitonic sequence (ex: k = 4, i = 0, 1, 2, 3, 8, 9, 10, 11)
441481
if (
442482
!(i & k) &&
443-
(await this.context.compare(arr, i, '>', l, drawIteration + 0.5))
483+
(await this.context.compare(
484+
arr,
485+
i,
486+
'>',
487+
l,
488+
options.parallel ? drawIteration + 0.5 : undefined,
489+
))
444490
) {
445-
await this.context.drawAndSwap(arr, i, l, drawIteration);
491+
await this.context.drawAndSwap(
492+
arr,
493+
i,
494+
l,
495+
options.parallel ? drawIteration : undefined,
496+
);
446497
}
447498
// i & k is true for the second half of the bitonic sequence (ex: k = 4, i = 4, 5, 6, 7, 12, 13, 14, 15)
448499
if (
449500
i & k &&
450-
(await this.context.compare(arr, i, '<', l, drawIteration + 0.5))
501+
(await this.context.compare(
502+
arr,
503+
i,
504+
'<',
505+
l,
506+
options.parallel ? drawIteration + 0.5 : undefined,
507+
))
451508
) {
452-
await this.context.drawAndSwap(arr, l, i, drawIteration);
509+
await this.context.drawAndSwap(
510+
arr,
511+
l,
512+
i,
513+
options.parallel ? drawIteration : undefined,
514+
);
453515
}
454516
});
455517
}
@@ -520,15 +582,37 @@ export class SortingAlgorithms {
520582
fns.push(async () => {
521583
if (
522584
direction === 'asc' &&
523-
(await this.context.compare(arr, i, '>', i + j, drawIteration + 0.5))
585+
(await this.context.compare(
586+
arr,
587+
i,
588+
'>',
589+
i + j,
590+
options.parallel ? drawIteration + 0.5 : undefined,
591+
))
524592
) {
525-
await this.context.drawAndSwap(arr, i, i + j, drawIteration);
593+
await this.context.drawAndSwap(
594+
arr,
595+
i,
596+
i + j,
597+
options.parallel ? drawIteration : undefined,
598+
);
526599
}
527600
if (
528601
direction === 'desc' &&
529-
(await this.context.compare(arr, i, '<', i + j, drawIteration + 0.5))
602+
(await this.context.compare(
603+
arr,
604+
i,
605+
'<',
606+
i + j,
607+
options.parallel ? drawIteration + 0.5 : undefined,
608+
))
530609
) {
531-
await this.context.drawAndSwap(arr, i, i + j, drawIteration);
610+
await this.context.drawAndSwap(
611+
arr,
612+
i,
613+
i + j,
614+
options.parallel ? drawIteration : undefined,
615+
);
532616
}
533617
});
534618
}

0 commit comments

Comments
 (0)