diff --git a/README.md b/README.md index 7a519d62f..8b7c338c9 100644 --- a/README.md +++ b/README.md @@ -249,6 +249,15 @@ +### _Uncategorized_ + +
+View contents + +* [`quickSort`](#quicksort) + +
+ ## Adapter ### call @@ -4002,6 +4011,33 @@ 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 dbc3d06cd..ccfd796a9 100644 --- a/docs/index.html +++ b/docs/index.html @@ -262,6 +262,9 @@ UUIDGenerator validateNumber +

Uncategorized +

quickSort +
 

Adapter

call

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

@@ -1776,6 +1779,24 @@ 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]
+