Skip to content

Commit

Permalink
set the record for properly documented function
Browse files Browse the repository at this point in the history
see quicksort
  • Loading branch information
make-github-pseudonymous-again committed Jan 14, 2015
1 parent bd8f0ad commit a4827ff
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 27 deletions.
54 changes: 41 additions & 13 deletions js/dist/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,31 +608,59 @@ exports.__mergesort__ = __mergesort__;

/**
* Template for the recursive implementation of quicksort.
* This template allows to generate a specific version of the quicksort
* algorithm for a certain partitioning algorithm.
*
*
* @param {callable} partition the implementation for the partitioning step
*/

var __quicksort__ = function ( partition ) {

var quicksort = function ( compare, a, i, j ) {

var p;
/**
* Sorts interval [left,right) of the array parameter according to a
* compare method.
*
* @param {comparator} compare the comparator function
* @param {array} array random access array
* @param {offset} left inner left bound of the interval to sort
* @param {offset} right outer right bound of the interval to sort
*
*/

if (j - i < 2) {
return;
}
var quicksort = function ( compare , array , left , right ) {

p = partition( compare, a, i, j );
var pivot ;

quicksort( compare, a, i, p );
quicksort( compare, a, p + 1, j );
};
// in the case where interval [left,right) contains
// only one element we are done!

return quicksort;
if ( right - left < 2 ) return ;

};
// otherwise we partition interval [left,right) into three disjoint
// subintervals [left,pivot), [pivot, pivot+1) and [pivot+1,right)
// where the pivot is the position whose element
// is greater or equal to all elements of the first subinterval
// and less or equal to all elements of the third subinterval

pivot = partition( compare , array , left , right ) ;

// and then we just need to ask the recursion fairy
// to sort the first and third subintervals

// the recursion fairy sorts [left,pivot)
quicksort( compare , array , left , pivot ) ;

// and then [pivot+1,right)
quicksort( compare , array , pivot + 1 , right ) ;

}

return quicksort ;

}

exports.__quicksort__ = __quicksort__;
exports.__quicksort__ = __quicksort__ ;

/* js/src/sort/selectionsort.js */

Expand Down
2 changes: 1 addition & 1 deletion js/dist/sort.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 41 additions & 13 deletions js/src/sort/quicksort.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,56 @@

/**
* Template for the recursive implementation of quicksort.
* This template allows to generate a specific version of the quicksort
* algorithm for a certain partitioning algorithm.
*
*
* @param {callable} partition the implementation for the partitioning step
*/

var __quicksort__ = function ( partition ) {

var quicksort = function ( compare, a, i, j ) {

var p;
/**
* Sorts interval [left,right) of the array parameter according to a
* compare method.
*
* @param {comparator} compare the comparator function
* @param {array} array random access array
* @param {offset} left inner left bound of the interval to sort
* @param {offset} right outer right bound of the interval to sort
*
*/

var quicksort = function ( compare , array , left , right ) {

var pivot ;

// in the case where interval [left,right) contains
// only one element we are done!

if ( right - left < 2 ) return ;

// otherwise we partition interval [left,right) into three disjoint
// subintervals [left,pivot), [pivot, pivot+1) and [pivot+1,right)
// where the pivot is the position whose element
// is greater or equal to all elements of the first subinterval
// and less or equal to all elements of the third subinterval

pivot = partition( compare , array , left , right ) ;

// and then we just need to ask the recursion fairy
// to sort the first and third subintervals

if (j - i < 2) {
return;
}
// the recursion fairy sorts [left,pivot)
quicksort( compare , array , left , pivot ) ;

p = partition( compare, a, i, j );
// and then [pivot+1,right)
quicksort( compare , array , pivot + 1 , right ) ;

quicksort( compare, a, i, p );
quicksort( compare, a, p + 1, j );
};
}

return quicksort;
return quicksort ;

};
}

exports.__quicksort__ = __quicksort__;
exports.__quicksort__ = __quicksort__ ;

0 comments on commit a4827ff

Please sign in to comment.