quick-sort.js
v1.0.1
Published
Generic quick sort implementation for js
Downloads
3
Maintainers
Readme
quick-sort.js - Generic quick sort implementation for js
quick-sort.js - Generic quick sort implementation for js. The algorithm takes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare. Quick sort on Wikipedia
##Installation
npm install quick-sort.js
###Usage
###Full example
var QSort = require('quick-sort.js');
var getter = function(iterable, index) {
return iterable[index];
};
var setter = function(iterable, value, index) {
iterable[index] = value;
};
var comparator = function(elem1, elem2) {
return elem2 - elem1;// desc sort
};
var list = [1,4,2,3,7,9,3,6,7,9,10,11,12,13,14,15,16,17,18,19,20];
console.log(list);
QSort(list, list.length, getter, setter, comparator);//desc sort
console.log(list);
####Simple example
var QSort = require('quick-sort.js');
var list = [1,4,2,3,7,9,3,6,7,9,10,11,12,13,14,15,16,17,18,19,20];
console.log(list);
QSort(list, list.length);//desc sort
console.log(list);
##API
####QSort(iterable, length, [[[fnGetter], [fnSetter]], [fnCompare]])
iterable
: object like arraylength
: length or iterablefnGetter
: function for getting a element from iterablefnGetter(iter, index)
- Optional, if not given, will use bracket syntaxe:
iter[index]
fnSetter
: function for setting a element from iterablefnGetter(iter, value,index)
- Optional, if not given, will use bracket syntaxe:
iter[index] = value
fnCompare(elem1, elem2)
: function for comparing two elem of iterable- Optional, if not given, will use :
elem1 - elem2
- Optional, if not given, will use :