Skip to content

Commit

Permalink
deploy to npm
Browse files Browse the repository at this point in the history
  • Loading branch information
cschen1205 committed May 24, 2017
1 parent a483cfe commit bdde4a9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ In terms of supported algorithms for sorting:
* Insertion Sort
* Merge Sort
* Quick Sort
* 3-Ways Quick Sort
* Heap Sort
* 3-Ways Quick Sort (WIP)
* Heap Sort (WIP)
* Shell Sort

# Install
Expand All @@ -37,6 +37,7 @@ jss.insertionSort(a);
jss.selectionSort(a);
jss.shellSort(a);
jss.mergeSort(a);
jss.quickSort(a);
```

Additionally user can specify the range in "a" to do the sorting as well as customized comparer:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-sorting-algorithms",
"version": "1.0.0",
"version": "1.0.1",
"description": "Package implements various array sorting algorithms",
"author": "Caishun Chen",
"contributors": [
Expand Down
20 changes: 10 additions & 10 deletions src/jssort.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ var jssort = jssort || {};
return;
}

var mid = lo + (hi - lo) / 2;
jss.mergeSort(a, aux, lo, mid, compare);
jss.mergeSort(a, aux, mid+1, hi, compare);
var mid = Math.floor(lo + (hi - lo) / 2);
jss.mergeSort(a, lo, mid, compare, aux);
jss.mergeSort(a, mid+1, hi, compare, aux);
jss.merge(a, aux, lo, mid, hi, compare);
};

Expand All @@ -118,21 +118,21 @@ var jssort = jssort || {};
aux[k] = a[k];
}

var i = lo, j = mid;
var i = lo, j = mid+1;
for (var k = lo; k <= hi; ++k) {
if ( i >= mid) {
if ( i > mid) {
a[k] = aux[j++];
}
if( j >= hi) {
else if( j > hi) {
a[k] = aux[i++];
}

if (jss.less(a[i], a[j], compare)) {
a[k] = a[i++];
else if (jss.less(aux[i], aux[j], compare)) {
a[k] = aux[i++];
} else {
a[k] = a[j++];
a[k] = aux[j++];
}
}

};

jss.quickSort = function (a, lo, hi, compare) {
Expand Down
37 changes: 37 additions & 0 deletions test/quick-sort-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var expect = require("chai").expect;
var jssort = require("../src/jssort");

describe("Quick Sort", function() {
describe("Sort Ascendingly", function() {

it("should sort ascedingly when no compare function is provided", function() {

var a = [3, 4, 5, 1, 2, 4, 6, 8, 9, 3, 4, 67, 34, 53, 44, 2];
jssort.quickSort(a);
for(var i = 1; i < a.length; ++i){
expect(a[i-1]).not.to.above(a[i]);
}
});

it("should sort ascedingly using the provided comparer", function() {

var a = [[3, 2.3], [4, 3.1], [5, 1.1], [1, 4.2], [2, 4.2], [4, 5.3], [6, 7.4], [8, 5.1], [9, 1.9], [3, 1.2], [4, 3.4], [67, 6.7], [34, 3], [53, 5], [44, 4.2], [2, 0]];
jssort.quickSort(a, undefined, undefined, function(a1, a2){
return a1[1] - a2[1];
});
for(var i = 1; i < a.length; ++i){
expect(a[i-1][1]).not.to.above(a[i][1]);
}
});


it("should sort ascedingly partially from index 3 to index 10 when no compare function is provided", function() {

var a = [3, 4, 5, 1, 2, 4, 6, 8, 9, 3, 4, 67, 34, 53, 44, 2];
jssort.quickSort(a, 3, 10);
for(var i = 4; i <= 10; ++i){
expect(a[i-1]).not.to.above(a[i]);
}
});
});
});

0 comments on commit bdde4a9

Please sign in to comment.