all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
|
|
@ -0,0 +1,30 @@
|
|||
function sort(array, less) {
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
|
||||
quicksort(left, right_new);
|
||||
quicksort(left_new, right);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
quicksort(0, array.length-1);
|
||||
|
||||
return array;
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
Array.prototype.quick_sort = function ()
|
||||
{
|
||||
if (this.length <= 1)
|
||||
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());
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue