diff --git a/README.md b/README.md index 8b7c338c9..46f01c489 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ * [`pull`](#pull) * [`pullAtIndex`](#pullatindex) * [`pullAtValue`](#pullatvalue) +* [`quickSort`](#quicksort) * [`remove`](#remove) * [`sample`](#sample) * [`shuffle`](#shuffle) @@ -249,15 +250,6 @@ -### _Uncategorized_ - -
-View contents - -* [`quickSort`](#quicksort) - -
- ## Adapter ### call @@ -1236,6 +1228,39 @@ console.log(pulled); // [ 'b', 'd' ] [⬆ Back to top](#table-of-contents) +### quickSort + +QuickSort an Array (ascending sort by default). + +Use recursion. +Use `Array.filter` and spread operator (`...`) to create an array that all elements with values less than the pivot come before the pivot, and all elements with values greater than the pivot come after it. +If the parameter `desc` is truthy, return array sorts in descending order. + +```js +const quickSort = ([n, ...nums], desc) => + isNaN(n) + ? [] + : [ + ...quickSort(nums.filter(v => (desc ? v > n : v <= n)), desc), + n, + ...quickSort(nums.filter(v => (!desc ? v > n : v <= n)), desc) + ]; +``` + +
+Examples + +```js +quickSort([4, 1, 3, 2]); // [1,2,3,4] +quickSort([4, 1, 3, 2], true); // [4,3,2,1] +``` + +
+ + +[⬆ Back to top](#table-of-contents) + + ### remove Removes elements from an array for which the given function returns `false`. @@ -4011,33 +4036,6 @@ validateNumber('10'); // true [⬆ Back to top](#table-of-contents) -## _Uncategorized_ - -### quickSort - -QuickSort an Array (ascending sort by default). - -Use recursion. -Use `Array.filter` and spread operator (`...`) to create an array that all elements with values less than the pivot come before the pivot, and all elements with values greater than the pivot come after it. -If the parameter `desc` is truthy, return array sorts in descending order. - -```js -const quickSort = ([n, ...nums], desc) => - isNaN(n) - ? [] - : [ - ...quickSort(nums.filter(v => (desc ? v > n : v <= n)), desc), - n, - ...quickSort(nums.filter(v => (!desc ? v > n : v <= n)), desc) - ]; -``` - -```js -quickSort([4, 1, 3, 2]); // [1,2,3,4] -quickSort([4, 1, 3, 2], true); // [4,3,2,1] -``` - -[⬆ back to top](#table-of-contents) ## Credits diff --git a/docs/index.html b/docs/index.html index ccfd796a9..1c26fcd01 100644 --- a/docs/index.html +++ b/docs/index.html @@ -138,6 +138,7 @@ pull pullAtIndex pullAtValue +quickSort remove sample shuffle @@ -262,9 +263,6 @@ UUIDGenerator validateNumber -

Uncategorized -

quickSort -
 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

@@ -648,6 +646,23 @@ let pulled = pullAtValue(myArray, ['b', 'd']); console.log(myArray); // [ 'a', 'c' ] console.log(pulled); // [ 'b', 'd' ] +

quickSort

+

QuickSort an Array (ascending sort by default).

+

Use recursion. +Use Array.filter and spread operator (...) to create an array that all elements with values less than the pivot come before the pivot, and all elements with values greater than the pivot come after it. +If the parameter desc is truthy, return array sorts in descending order.

+
const quickSort = ([n, ...nums], desc) =>
+  isNaN(n)
+    ? []
+    : [
+        ...quickSort(nums.filter(v => (desc ? v > n : v <= n)), desc),
+        n,
+        ...quickSort(nums.filter(v => (!desc ? v > n : v <= n)), desc)
+      ];
+
+
quickSort([4, 1, 3, 2]); // [1,2,3,4]
+quickSort([4, 1, 3, 2], true); // [4,3,2,1]
+

remove

Removes elements from an array for which the given function returns false.

Use Array.filter() to find array elements that return truthy values and Array.reduce() to remove elements using Array.splice(). @@ -1779,24 +1794,6 @@ Use Number() to check if the coercion holds.

validateNumber('10'); // true
 
-

Uncategorized

-

quickSort

-

QuickSort an Array (ascending sort by default).

-

Use recursion. -Use Array.filter and spread operator (...) to create an array that all elements with values less than the pivot come before the pivot, and all elements with values greater than the pivot come after it. -If the parameter desc is truthy, return array sorts in descending order.

-
const quickSort = ([n, ...nums], desc) =>
-  isNaN(n)
-    ? []
-    : [
-        ...quickSort(nums.filter(v => (desc ? v > n : v <= n)), desc),
-        n,
-        ...quickSort(nums.filter(v => (!desc ? v > n : v <= n)), desc)
-      ];
-
-
quickSort([4, 1, 3, 2]); // [1,2,3,4]
-quickSort([4, 1, 3, 2], true); // [4,3,2,1]
-