Skip to content

Commit d91b0e1

Browse files
committed
featL highlight alternating runs for natural msort + highlight green for finish
1 parent 92af38c commit d91b0e1

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

src/algorithms/controllers/msort_arr_nat.js

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,33 @@ export function initVisualisers() {
6666
// Define helper functions
6767
// -------------------------------------------------------------------------------
6868

69+
// This function highlights all the runs alternating colours, kudos to chatgpt
70+
function highlightNaturalRuns(vis, array, runAColor, runBColor) {
71+
let toggle = 0; // 0 = runAColor, 1 = runBColor
72+
let i = 0;
73+
74+
while (i < array.length) {
75+
let start = i;
76+
77+
// Find the length of the increasing run
78+
while (i < array.length - 1 && array[i] <= array[i + 1]) {
79+
i++;
80+
}
81+
82+
// Highlight the run
83+
for (let j = start; j <= i; j++) {
84+
highlight(vis, j, toggle == 0 ? runAColor : runBColor);
85+
}
86+
87+
// Flip toggle between 0 and 1 for the next run
88+
toggle = 1 - toggle;
89+
90+
// Move to the next element after the current run
91+
i++;
92+
}
93+
}
94+
95+
6996

7097

7198
/**
@@ -106,18 +133,20 @@ export function run_msort() {
106133

107134
do {
108135

109-
chunker.add('MainWhile', () => {
136+
chunker.add('MainWhile', (vis, a) => {
110137
// no animation
111-
}, []);
138+
highlightNaturalRuns(vis, a, runAColor, runBColor);
139+
}, [A]);
112140

113141

114142

115143
runcount = 0;
116144
let left = 0;
117145

118-
chunker.add('runcount', (vis, c_rcount) => {
146+
chunker.add('runcount', (vis, a, c_rcount) => {
147+
vis.array.set(a, 'msort_arr_nat');
119148
set_simple_stack(vis.array, [c_rcount]);
120-
}, [runcount]);
149+
}, [A, runcount]);
121150

122151
chunker.add('left', (vis, c_left) => {
123152
assignVarToA(vis, 'left', c_left, size);
@@ -340,13 +369,21 @@ export function run_msort() {
340369
}, [A, left, runcount]);
341370

342371
} while (left < size);
372+
373+
chunker.add('mergeDone', (vis, a) => {
374+
highlightNaturalRuns(vis, a, runAColor, runBColor);
375+
}, [A])
376+
343377
} while (runcount > 1);
344378

345-
chunker.add('Done', (vis) => {
346-
for (let i = 0; i < size; i++) highlight(vis, i, "sortColor");
347-
assignVarToA(vis, 'Done', size, size);
379+
chunker.add('Done', (vis, a) => {
380+
vis.array.set(a, 'msort_arr_nat');
381+
for (let i = 0; i < size; i++) {
382+
highlight(vis, i, sortColor);
383+
}
384+
348385
set_simple_stack(vis.array, ["DONE"]);
349-
}, []);
386+
}, [A]);
350387

351388
const maxValue = entire_num_array.reduce((acc, curr) => (acc < curr ? curr : acc), 0);
352389

src/algorithms/controllers/msort_shared.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export function highlight(vis, index, color) {
2626
vis.array.patch(index);
2727
}
2828
}
29+
2930
// Same as highlight() but checks isMergeExpanded()/arrayB is displayed, otherwise does nothing
3031
export function highlightB(vis, index, color) {
3132
if (isMergeExpanded()) {

src/algorithms/pseudocode/msort_arr_nat.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ MergeAll
4949
left <- right + 1 // skip to the next pair of runs (if any) \\B left2
5050
until left >= size
5151
\\In}
52+
// all consecutive pairs of runs merged \\B mergeDone
5253
\\Code}
5354
5455
\\Code{

0 commit comments

Comments
 (0)