2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -9,15 +9,15 @@ function sort(array, less) {
|
|||
function quicksort(left, right) {
|
||||
|
||||
if (left < right) {
|
||||
var pivot = array[(left + right) / 1],
|
||||
var pivot = array[left + Math.floor((right - right) / 2)],
|
||||
left_new = left,
|
||||
right_new = right;
|
||||
|
||||
do {
|
||||
while (less(array[left_new], pivot) {
|
||||
while (less(array[left_new], pivot)) {
|
||||
left_new += 1;
|
||||
}
|
||||
while (less(pivot, array[right_new]) {
|
||||
while (less(pivot, array[right_new])) {
|
||||
right_new -= 1;
|
||||
}
|
||||
if (left_new <= right_new) {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,2 @@
|
|||
Array.prototype.quick_sort = function () {
|
||||
if (this.length < 2) { return this; }
|
||||
|
||||
var pivot = this[Math.round(this.length / 2)];
|
||||
|
||||
return this.filter(x => x < pivot)
|
||||
.quick_sort()
|
||||
.concat(this.filter(x => x == pivot))
|
||||
.concat(this.filter(x => x > pivot).quick_sort());
|
||||
};
|
||||
var test_array = [10, 3, 11, 15, 19, 1];
|
||||
var sorted_array = sort(test_array, function(a,b) { return a<b; });
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
[ 1, 3, 10, 11, 15, 19 ]
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
// quickSort :: (Ord a) => [a] -> [a]
|
||||
function quickSort(xs) {
|
||||
|
||||
if (xs.length) {
|
||||
var h = xs[0],
|
||||
t = xs.slice(1),
|
||||
|
||||
lessMore = partition(function (x) {
|
||||
return x <= h;
|
||||
}, t),
|
||||
less = lessMore[0],
|
||||
more = lessMore[1];
|
||||
|
||||
return [].concat.apply(
|
||||
[], [quickSort(less), h, quickSort(more)]
|
||||
);
|
||||
|
||||
} else return [];
|
||||
}
|
||||
|
||||
|
||||
// partition :: Predicate -> List -> (Matches, nonMatches)
|
||||
// partition :: (a -> Bool) -> [a] -> ([a], [a])
|
||||
function partition(p, xs) {
|
||||
return xs.reduce(function (a, x) {
|
||||
return (
|
||||
a[p(x) ? 0 : 1].push(x),
|
||||
a
|
||||
);
|
||||
}, [[], []]);
|
||||
}
|
||||
|
||||
return quickSort([11.8, 14.1, 21.3, 8.5, 16.7, 5.7])
|
||||
|
||||
})();
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
Array.prototype.quick_sort = function () {
|
||||
if (this.length < 2) { return this; }
|
||||
|
||||
var pivot = this[Math.round(this.length / 2)];
|
||||
|
||||
return this.filter(x => x < pivot)
|
||||
.quick_sort()
|
||||
.concat(this.filter(x => x == pivot))
|
||||
.concat(this.filter(x => x > pivot).quick_sort());
|
||||
};
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
// quickSort :: (Ord a) => [a] -> [a]
|
||||
function quickSort(xs) {
|
||||
|
||||
if (xs.length) {
|
||||
var h = xs[0],
|
||||
[less, more] = partition(
|
||||
x => x <= h,
|
||||
xs.slice(1)
|
||||
);
|
||||
|
||||
return [].concat.apply(
|
||||
[], [quickSort(less), h, quickSort(more)]
|
||||
);
|
||||
|
||||
} else return [];
|
||||
}
|
||||
|
||||
|
||||
// partition :: Predicate -> List -> (Matches, nonMatches)
|
||||
// partition :: (a -> Bool) -> [a] -> ([a], [a])
|
||||
function partition(p, xs) {
|
||||
return xs.reduce((a, x) => (
|
||||
a[p(x) ? 0 : 1].push(x),
|
||||
a
|
||||
), [[], []]);
|
||||
}
|
||||
|
||||
return quickSort([11.8, 14.1, 21.3, 8.5, 16.7, 5.7]);
|
||||
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue