forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
34 lines (28 loc) · 1.04 KB
/
code.js
File metadata and controls
34 lines (28 loc) · 1.04 KB
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
logger._print('original array = [' + D.join(', ') + ']');
var N = D.length;
var swapped;
var gap = N; // initialize gap size
var shrink = 1.3; // set the gap shrink factor
do{
// update the gap value for the next comb.
gap = Math.floor( gap/shrink );
if( gap < 1 ){
// minimum gap is 1
gap = 1;
}
swapped = false; // initialize swapped
// a single comb over the input list
for( var i=0; i+gap < N; i++ ){
tracer._select(i)._select(i+gap)._wait();
if( D[i] > D[i+gap] ){
logger._print('swap ' + D[i] + ' and ' + D[i+gap]); // log swap event
var temp = D[i];
D[i] = D[i+gap];
D[i+gap] = temp;
tracer._notify(i, D[i])._notify(i+gap, D[i+gap])._wait();
tracer._denotify(i)._denotify(i+gap);
swapped = true; // Flag swapped has happened and list is not guaranteed sorted
}
tracer._deselect(i)._deselect(i+gap);
} // End of combing
} while( gap!=1 || swapped )