Skip to content

Commit b508c7c

Browse files
commenting Hwang-Lin
1 parent 95172dc commit b508c7c

File tree

4 files changed

+124
-60
lines changed

4 files changed

+124
-60
lines changed

js/dist/sort.js

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,87 @@
77
/* js/src/merge/binarymerge.js */
88

99

10-
/**
11-
* Merges 2 arrays using the Hwang Lin algorithm.
12-
*
13-
* /!\ aj - ai >= bj - bi
14-
*/
10+
var __binarymerge__ = function ( binarysearch , copy ) {
1511

16-
var __binarymerge__ = function ( binarysearch, copy ) {
12+
/**
13+
* Merges 2 arrays using the Hwang Lin algorithm.
14+
*
15+
* /!\ aj - ai >= bj - bi
16+
*/
1717

18-
var hwanglin = function ( compare, a, ai, aj, b, bi, bj, c, ci ) {
18+
var hwanglin = function ( compare , a , ai , aj , b , bi , bj , c , ci ) {
1919

20-
var o, t, x, q, d, z;
20+
var m , n , o , t , g , q , d , z ;
2121

22-
o = ci - ai - bi;
23-
t = ai;
22+
o = ci - ai - bi ;
23+
t = ai ;
2424

25-
x = Math.pow( 2, Math.floor( Math.log( ( aj - ai ) / ( bj - bi ) ) / Math.log( 2 ) ) );
25+
m = aj - ai ;
26+
n = bj - ai ;
2627

27-
while ( bi < bj && ( ai + x < aj || ( x = aj - ai - 1 ) ) ) {
28+
// g is the size of a block
2829

29-
t = ai;
30-
ai = t + x;
30+
g = Math.pow( 2 , Math.floor( Math.log( m / n ) / Math.log( 2 ) ) ) ;
31+
32+
blocks : while ( bi < bj && ( ai + g < aj || ( g = aj - ai - 1 ) ) ) {
33+
34+
// for each block
35+
36+
// t is the inner left bound
37+
t = ai ;
38+
39+
// ai is the inner right bound
40+
ai = t + g ;
3141

3242
while ( bi < bj ) {
3343

34-
if ( compare( b[bi], a[ai] ) >= 0 ) {
35-
copy( a, t, ai, c, o + t + bi );
36-
break;
44+
// we will try to insert b_i in this block
45+
46+
if ( compare( b[bi] , a[ai] ) >= 0 ) {
47+
48+
// b_i must be inserted to the left of this block
49+
// we copy the block to the output array and continue
50+
// with the next block
51+
copy( a , t , ai , c , o + t + bi ) ;
52+
continue blocks ;
53+
3754
}
3855

39-
q = binarysearch( compare, a, t, ai, b[bi] );
40-
z = q[0] + q[1];
56+
// b_i is inside this block and smaller than a_i
57+
// we search its insertion position using a binary search algorithm
58+
q = binarysearch( compare , a , t , ai , b[bi] ) ;
59+
60+
// z is the insertion position
61+
z = q[0] + q[1] ;
62+
63+
// copy everything to the left of the insertion position
64+
// to the output array
65+
copy( a , t , z , c , o + t + bi ) ;
66+
67+
// copy b_i to its insertion position in the output array
68+
c[o + bi + z] = b[bi] ;
69+
70+
// update the inner left bound of the block
71+
t = z ;
72+
73+
// go to the next b_i
74+
++bi ;
4175

42-
copy( a, t, z, c, o + t + bi );
43-
c[o + z + bi] = b[bi];
44-
t = z;
45-
++bi;
4676
}
77+
4778
}
4879

49-
copy( a, t, aj, c, o + t + bi );
50-
copy( b, bi, bj, c, o + aj + bi );
80+
// remaining elements are copied to the output array
81+
copy( a , t , aj , c , o + t + bi ) ;
82+
copy( b , bi , bj , c , o + aj + bi ) ;
5183

52-
};
84+
} ;
5385

54-
return hwanglin;
86+
return hwanglin ;
5587

56-
};
88+
} ;
5789

58-
exports.__binarymerge__ = __binarymerge__;
90+
exports.__binarymerge__ = __binarymerge__ ;
5991

6092
/* js/src/merge/merge.js */
6193

0 commit comments

Comments
 (0)