|
1 | | -logger._print( 'original array = [' + D.join(', ') + ']' ); |
| 1 | +logger._print('original array = [' + D.join(', ') + ']'); |
2 | 2 | var N = D.length; |
3 | 3 | var writes = 0; |
4 | 4 | var pos; |
5 | 5 | var item; |
6 | 6 | var temp; |
7 | | -for(var cycleStart=0; cycleStart<=N-2; cycleStart++ ){ |
8 | | - item = D[cycleStart]; |
9 | | - pos = cycleStart; |
10 | | - tracer._select(cycleStart); |
| 7 | +for (var cycleStart = 0; cycleStart <= N - 2; cycleStart++) { |
| 8 | + item = D[cycleStart]; |
| 9 | + pos = cycleStart; |
| 10 | + tracer._select(cycleStart); |
11 | 11 |
|
12 | | - for(var i=cycleStart+1; i<=N-1; i++ ){ |
13 | | - tracer._select(i)._wait()._deselect(i); |
14 | | - if( D[i]<item ){ |
15 | | - pos++; |
16 | | - } |
17 | | - } |
18 | | - if( pos == cycleStart ){ |
19 | | - tracer._deselect(cycleStart); |
20 | | - continue; |
| 12 | + for (var i = cycleStart + 1; i <= N - 1; i++) { |
| 13 | + tracer._select(i)._wait()._deselect(i); |
| 14 | + if (D[i] < item) { |
| 15 | + pos++; |
21 | 16 | } |
22 | | - while( item == D[pos] ){ |
| 17 | + } |
| 18 | + if (pos == cycleStart) { |
| 19 | + tracer._deselect(cycleStart); |
| 20 | + continue; |
| 21 | + } |
| 22 | + while (item == D[pos]) { |
| 23 | + pos++; |
| 24 | + } |
| 25 | + |
| 26 | + temp = D[pos]; |
| 27 | + D[pos] = item; |
| 28 | + item = temp; |
| 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(); |
| 37 | + tracer._denotify(pos)._denotify(cycleStart); |
| 38 | + |
| 39 | + while (pos != cycleStart) { |
| 40 | + pos = cycleStart; |
| 41 | + |
| 42 | + for (i = cycleStart + 1; i <= N - 1; i++) { |
| 43 | + tracer._select(i)._wait()._deselect(i); |
| 44 | + if (D[i] < item) { |
23 | 45 | pos++; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + while (item == D[pos]) { |
| 50 | + pos++; |
24 | 51 | } |
25 | 52 |
|
26 | 53 | temp = D[pos]; |
27 | 54 | D[pos] = item; |
28 | 55 | item = temp; |
29 | 56 |
|
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); |
| 57 | + if (pos !== cycleStart) { |
| 58 | + logger._print('Rewrite ' + D[pos] + ' to index ' + pos + '; the next value to rewrite is ' + item); |
| 59 | + } else { |
| 60 | + logger._print('Rewrite ' + D[pos] + ' to index ' + pos); |
34 | 61 | } |
35 | 62 | tracer._select(pos)._wait()._deselect(pos); |
36 | 63 | tracer._notify(pos, D[pos])._notify(cycleStart, D[cycleStart])._wait(); |
37 | 64 | tracer._denotify(pos)._denotify(cycleStart); |
38 | 65 |
|
39 | | - while( pos != cycleStart ){ |
40 | | - pos = cycleStart; |
41 | | - |
42 | | - for( i=cycleStart+1; i<=N-1; i++ ){ |
43 | | - tracer._select(i)._wait()._deselect(i); |
44 | | - if( D[i]<item ){ |
45 | | - pos++; |
46 | | - } |
47 | | - } |
48 | | - |
49 | | - while( item == D[pos] ){ |
50 | | - pos++; |
51 | | - } |
52 | | - |
53 | | - temp = D[pos]; |
54 | | - D[pos] = item; |
55 | | - item = temp; |
56 | | - |
57 | | - if( pos !== cycleStart ){ |
58 | | - logger._print( 'Rewrite '+D[pos]+' to index '+pos+'; the next value to rewrite is '+item ); |
59 | | - }else{ |
60 | | - logger._print( 'Rewrite '+D[pos]+' to index '+pos); |
61 | | - } |
62 | | - tracer._select(pos)._wait()._deselect(pos); |
63 | | - tracer._notify(pos, D[pos])._notify(cycleStart, D[cycleStart])._wait(); |
64 | | - tracer._denotify(pos)._denotify(cycleStart); |
65 | | - |
66 | | - writes++; |
67 | | - } |
68 | | - |
69 | 66 | writes++; |
| 67 | + } |
| 68 | + |
| 69 | + writes++; |
70 | 70 | } |
71 | 71 |
|
72 | | -logger._print( 'Number of writes performed is ' + writes ); |
| 72 | +logger._print('Number of writes performed is ' + writes); |
0 commit comments