Skip to content

Commit 7f4b581

Browse files
committed
unit test for 3-ways quick sort
1 parent f94e574 commit 7f4b581

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ In terms of supported algorithms for sorting:
1616
* Insertion Sort
1717
* Merge Sort
1818
* Quick Sort
19-
* 3-Ways Quick Sort (WIP)
19+
* 3-Ways Quick Sort
2020
* Heap Sort (WIP)
2121
* Shell Sort
2222

src/jssort.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ var jssort = jssort || {};
204204

205205
var i = lo, lt = lo, gt = hi;
206206
var v = a[lo];
207-
while (i < gt) {
207+
while (i <= gt) {
208208
if (jss.less(a[i], v, compare)) {
209209
jss.exchange(a, i++, lt++);
210210
} else if(jss.less(v, a[i], compare)) {

test/three-ways-quick-sort-spec.js

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

0 commit comments

Comments
 (0)