Skip to content

Commit 30754e3

Browse files
draft for Ford-Johnson sorting algorithm
1 parent 26c6f6f commit 30754e3

File tree

4 files changed

+203
-5
lines changed

4 files changed

+203
-5
lines changed

js/dist/sort.js

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
(function(exports, undefined){
1+
( function ( ) {
22

3-
'use strict';
3+
'use strict' ;
4+
5+
var definition = function ( exports , undefined ) {
46

57

68
/* js/src/merge */
@@ -468,6 +470,99 @@ var __dualpivotquicksort__ = function ( partition ) {
468470

469471
exports.__dualpivotquicksort__ = __dualpivotquicksort__;
470472

473+
/* js/src/sort/fordjohnson.js */
474+
475+
var fordjohnson = function ( compare , swap , a , i , j ) {
476+
477+
var m , k , t , y , p , q , r , x , l , w , s , pairswap ;
478+
479+
k = m = ( j - i ) / 2 | 0 ;
480+
481+
// compare pairs of elements and put largest elements at the front of the
482+
// array
483+
484+
while ( k-- ) {
485+
486+
if ( compare( a[i+k] , a[i+m+k] ) < 0 ) {
487+
488+
swap( a , i + k , i + m + k ) ;
489+
490+
}
491+
492+
}
493+
494+
// sort the largest elements at the front recursively
495+
496+
pairswap = function ( a , i , j ) {
497+
swap( a , i , j ) ;
498+
swap( a , i + m , j + m ) ;
499+
} ;
500+
501+
fordjohnson( compare , pairswap , a , i , i + m ) ;
502+
503+
// merge the rest of the array into the front, one item at a time
504+
505+
p = y = t = 1 ;
506+
507+
q = 0 ;
508+
509+
while ( i + m + t <= j ) {
510+
511+
r = t ;
512+
513+
while ( r --> q ) {
514+
515+
w = a[i+m+t-1] ;
516+
517+
x = binarysearch( compare , a , i , i + t - 1 , w ) ;
518+
l = x[0] + x[1] ;
519+
520+
swap( a , i + m + q , i + m + t - 1 ) ;
521+
522+
s = i + m + t ;
523+
524+
while ( --s > l ) {
525+
526+
swap( a , s , s - 1 ) ;
527+
528+
}
529+
530+
}
531+
532+
q = t ;
533+
534+
p *= 2 ;
535+
y = p - 2 * t ;
536+
t += y ;
537+
538+
}
539+
540+
r = j - i - m ;
541+
542+
while ( r --> q ) {
543+
544+
w = a[i+m+t-1] ;
545+
546+
x = binarysearch( compare , a , i , i + t - 1 , w ) ;
547+
l = x[0] + x[1] ;
548+
549+
swap( a , i + m + q , i + m + t - 1 ) ;
550+
551+
s = i + m + t ;
552+
553+
while ( --s > l ) {
554+
555+
swap( a , s , s - 1 ) ;
556+
557+
}
558+
559+
}
560+
561+
562+
} ;
563+
564+
exports.fordjohnson = fordjohnson ;
565+
471566
/* js/src/sort/heapsort.js */
472567

473568

@@ -919,4 +1014,16 @@ var whole = function ( sort ) {
9191014

9201015
exports.whole = whole;
9211016

922-
})(typeof exports === 'undefined' ? this['sort'] = {} : exports);
1017+
return exports ;
1018+
} ;
1019+
if ( typeof exports === "object" ) {
1020+
definition( exports ) ;
1021+
}
1022+
else if ( typeof define === "function" && define.amd ) {
1023+
define( "author-js-lib" , [ ] , function ( ) { return definition( { } ) ; } ) ;
1024+
}
1025+
else if ( typeof window === "object" && typeof window.document === "object" ) {
1026+
definition( window["sort"] = { } ) ;
1027+
}
1028+
else console.error( "unable to detect type of module to define for author-js-lib") ;
1029+
} )( ) ;

0 commit comments

Comments
 (0)