Skip to content

Commit bdde4a9

Browse files
committed
deploy to npm
1 parent a483cfe commit bdde4a9

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ In terms of supported algorithms for sorting:
1616
* Insertion Sort
1717
* Merge Sort
1818
* Quick Sort
19-
* 3-Ways Quick Sort
20-
* Heap Sort
19+
* 3-Ways Quick Sort (WIP)
20+
* Heap Sort (WIP)
2121
* Shell Sort
2222

2323
# Install
@@ -37,6 +37,7 @@ jss.insertionSort(a);
3737
jss.selectionSort(a);
3838
jss.shellSort(a);
3939
jss.mergeSort(a);
40+
jss.quickSort(a);
4041
```
4142

4243
Additionally user can specify the range in "a" to do the sorting as well as customized comparer:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-sorting-algorithms",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Package implements various array sorting algorithms",
55
"author": "Caishun Chen",
66
"contributors": [

src/jssort.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ var jssort = jssort || {};
107107
return;
108108
}
109109

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

@@ -118,21 +118,21 @@ var jssort = jssort || {};
118118
aux[k] = a[k];
119119
}
120120

121-
var i = lo, j = mid;
121+
var i = lo, j = mid+1;
122122
for (var k = lo; k <= hi; ++k) {
123-
if ( i >= mid) {
123+
if ( i > mid) {
124124
a[k] = aux[j++];
125125
}
126-
if( j >= hi) {
126+
else if( j > hi) {
127127
a[k] = aux[i++];
128128
}
129-
130-
if (jss.less(a[i], a[j], compare)) {
131-
a[k] = a[i++];
129+
else if (jss.less(aux[i], aux[j], compare)) {
130+
a[k] = aux[i++];
132131
} else {
133-
a[k] = a[j++];
132+
a[k] = aux[j++];
134133
}
135134
}
135+
136136
};
137137

138138
jss.quickSort = function (a, lo, hi, compare) {

test/quick-sort-spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var expect = require("chai").expect;
2+
var jssort = require("../src/jssort");
3+
4+
describe("Quick Sort", function() {
5+
describe("Sort Ascendingly", function() {
6+
7+
it("should sort ascedingly when no compare function is provided", function() {
8+
9+
var a = [3, 4, 5, 1, 2, 4, 6, 8, 9, 3, 4, 67, 34, 53, 44, 2];
10+
jssort.quickSort(a);
11+
for(var i = 1; i < a.length; ++i){
12+
expect(a[i-1]).not.to.above(a[i]);
13+
}
14+
});
15+
16+
it("should sort ascedingly using the provided comparer", function() {
17+
18+
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]];
19+
jssort.quickSort(a, undefined, undefined, function(a1, a2){
20+
return a1[1] - a2[1];
21+
});
22+
for(var i = 1; i < a.length; ++i){
23+
expect(a[i-1][1]).not.to.above(a[i][1]);
24+
}
25+
});
26+
27+
28+
it("should sort ascedingly partially from index 3 to index 10 when no compare function is provided", function() {
29+
30+
var a = [3, 4, 5, 1, 2, 4, 6, 8, 9, 3, 4, 67, 34, 53, 44, 2];
31+
jssort.quickSort(a, 3, 10);
32+
for(var i = 4; i <= 10; ++i){
33+
expect(a[i-1]).not.to.above(a[i]);
34+
}
35+
});
36+
});
37+
});

0 commit comments

Comments
 (0)