Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -1,22 +1,31 @@
|
|||
function sort(array, less) {
|
||||
|
||||
function swap(i, j) { var t=array[i]; array[i]=array[j]; array[j]=t }
|
||||
function swap(i, j) {
|
||||
var t = array[i];
|
||||
array[i] = array[j];
|
||||
array[j] = t;
|
||||
}
|
||||
|
||||
function quicksort(left, right) {
|
||||
|
||||
if (left < right) {
|
||||
|
||||
var pivot = array[(left + right) >> 1];
|
||||
var left_new = left, right_new = right;
|
||||
var pivot = array[(left + right) / 1],
|
||||
left_new = left,
|
||||
right_new = right;
|
||||
|
||||
do {
|
||||
while (less(array[left_new], pivot)
|
||||
left_new++;
|
||||
while (less(pivot, array[right_new])
|
||||
right_new--;
|
||||
if (left_new <= right_new)
|
||||
swap(left_new++, right_new--);
|
||||
} while (left_new <= right_new);
|
||||
while (less(array[left_new], pivot) {
|
||||
left_new += 1;
|
||||
}
|
||||
while (less(pivot, array[right_new]) {
|
||||
right_new -= 1;
|
||||
}
|
||||
if (left_new <= right_new) {
|
||||
swap(left_new, right_new);
|
||||
left_new += 1;
|
||||
right_new -= 1;
|
||||
}
|
||||
} while (left_new <= right_new);
|
||||
|
||||
quicksort(left, right_new);
|
||||
quicksort(left_new, right);
|
||||
|
|
@ -24,7 +33,7 @@ function sort(array, less) {
|
|||
}
|
||||
}
|
||||
|
||||
quicksort(0, array.length-1);
|
||||
quicksort(0, array.length - 1);
|
||||
|
||||
return array;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
Array.prototype.quick_sort = function ()
|
||||
{
|
||||
if (this.length <= 1)
|
||||
return this;
|
||||
Array.prototype.quick_sort = function () {
|
||||
if (this.length < 2) { return this; }
|
||||
|
||||
var pivot = this[Math.round(this.length / 2)];
|
||||
|
||||
return this.filter(function (x) { return x < pivot }).quick_sort().concat(
|
||||
this.filter(function (x) { return x == pivot })).concat(
|
||||
this.filter(function (x) { return x > pivot }).quick_sort());
|
||||
}
|
||||
return this.filter(x => x < pivot)
|
||||
.quick_sort()
|
||||
.concat(this.filter(x => x == pivot))
|
||||
.concat(this.filter(x => x > pivot).quick_sort());
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue