Skip to content

Commit bd0d44b

Browse files
Hui ChenHui Chen
Hui Chen
authored and
Hui Chen
committed
improved tracer visuals for combsort and cyclesort
1 parent a810eb9 commit bd0d44b

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

algorithm/sorting/comb/basic/code.js

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ do{
1515
swapped = false; // initialize swapped
1616
// a single comb over the input list
1717
for( var i=0; i+gap < N; i++ ){
18+
tracer._select(i)._select(i+gap)._wait();
19+
1820
if( D[i] > D[i+gap] ){
1921
logger._print('swap ' + D[i] + ' and ' + D[i+gap]); // log swap event
2022

@@ -27,5 +29,6 @@ do{
2729

2830
swapped = true; // Flag swapped has happened and list is not guaranteed sorted
2931
}
32+
tracer._deselect(i)._deselect(i+gap);
3033
} // End of combing
3134
} while( gap!=1 || swapped )

algorithm/sorting/cycle/basic/code.js

+22-9
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,39 @@ var temp;
77
for( cycleStart=0; cycleStart<=N-2; cycleStart++ ){
88
item = D[cycleStart];
99
pos = cycleStart;
10+
tracer._select(cycleStart);
11+
1012
for( i=cycleStart+1; i<=N-1; i++ ){
13+
tracer._select(i)._wait()._deselect(i);
1114
if( D[i]<item ){
1215
pos++;
1316
}
1417
}
1518
if( pos == cycleStart ){
19+
tracer._deselect(cycleStart);
1620
continue;
1721
}
1822
while( item == D[pos] ){
1923
pos++;
2024
}
25+
2126
temp = D[pos];
2227
D[pos] = item;
2328
item = temp;
2429

25-
logger._print( 'Rewrite '+D[pos]+' to index '+pos );
26-
27-
tracer._notify(pos, D[pos])._notify(cycleStart, D[cycleStart])._wait();
28-
tracer._denotify(pos)._denotify(pos);
29-
30+
if( pos !== cycleStart ){
31+
logger._print( 'Rewrite '+D[pos]+' to index '+pos+'; the next value to rewrite is '+item );
32+
}else{
33+
logger._print( 'Rewrite '+D[pos]+' to index '+pos);
34+
}
35+
tracer._select(pos)._wait()._deselect(pos);
36+
tracer._notify(pos, D[pos])._notify(cycleStart, D[cycleStart])._wait()._denotify(pos)._denotify(cycleStart);
3037

3138
while( pos != cycleStart ){
3239
pos = cycleStart;
40+
3341
for( i=cycleStart+1; i<=N-1; i++ ){
42+
tracer._select(i)._wait()._deselect(i);
3443
if( D[i]<item ){
3544
pos++;
3645
}
@@ -39,14 +48,18 @@ for( cycleStart=0; cycleStart<=N-2; cycleStart++ ){
3948
while( item == D[pos] ){
4049
pos++;
4150
}
51+
4252
temp = D[pos];
4353
D[pos] = item;
4454
item = temp;
4555

46-
logger._print( 'Rewrite '+D[pos]+' to index '+pos );
47-
48-
tracer._notify(pos, D[pos])._notify(cycleStart, D[cycleStart])._wait();
49-
tracer._denotify(pos)._denotify(pos);
56+
if( pos !== cycleStart ){
57+
logger._print( 'Rewrite '+D[pos]+' to index '+pos+'; the next value to rewrite is '+item );
58+
}else{
59+
logger._print( 'Rewrite '+D[pos]+' to index '+pos);
60+
}
61+
tracer._select(pos)._wait()._deselect(pos);
62+
tracer._notify(pos, D[pos])._notify(cycleStart, D[cycleStart])._wait()._denotify(pos)._denotify(cycleStart);
5063

5164
writes++;
5265
}

0 commit comments

Comments
 (0)