Skip to content

Commit

Permalink
3 ways quick sort
Browse files Browse the repository at this point in the history
  • Loading branch information
cschen1205 committed May 25, 2017
1 parent bdde4a9 commit f94e574
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/jssort.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,40 @@ var jssort = jssort || {};
jss.exchange(a, lo, j);
return j;
};

jss.threeWaysQuickSort = function (a, lo, hi, compare) {
if (!lo) lo = 0;
if (!hi) hi = a.length-1;
if (!compare) {
compare = function (a1, a2){
return a1 - a2;
};
}

if (lo >= hi) {
return;
}

if (hi - lo <= 7) {
jss.insertionSort(a, lo, hi, compare);
return;
}

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

jss.threeWaysQuickSort(a, lo, lt-1, compare);
jss.threeWaysQuickSort(a, gt+1, hi, compare);
};

})(jssort);

Expand Down

0 comments on commit f94e574

Please sign in to comment.