Skip to content

Commit b88501c

Browse files
committed
unit test for shell sort
1 parent 8c2473d commit b88501c

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/jssort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ var jssort = jssort || {};
7373
var step = h;
7474
while (step >= 1) {
7575
for (var i = lo + step; i <= hi; i++){
76-
for(var j = i; j >= lo; j -= step) {
76+
for(var j = i; j >= lo + step; j -= step) {
7777
if(jss.less(a[j], a[j-step], compare)){
7878
jss.exchange(a, j, j-step);
7979
} else {

test/shell-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("Shell 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.shellSort(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.shellSort(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.shellSort(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)