From 5892f8571cf677d6465dbd7533aac05eb753401c Mon Sep 17 00:00:00 2001 From: xqdoo00o Date: Thu, 28 Dec 2017 19:19:33 +0800 Subject: [PATCH 1/3] Create arrayQuickSort.md --- snippets/arrayQuickSort.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 snippets/arrayQuickSort.md diff --git a/snippets/arrayQuickSort.md b/snippets/arrayQuickSort.md new file mode 100644 index 000000000..cbc5f73c0 --- /dev/null +++ b/snippets/arrayQuickSort.md @@ -0,0 +1,21 @@ +### ArrayQuickSort + +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] +``` From 3bae95bc2b5b938ae45ede447377f055985b9068 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 28 Dec 2017 13:42:41 +0200 Subject: [PATCH 2/3] Update and rename arrayQuickSort.md to quickSort.md --- snippets/{arrayQuickSort.md => quickSort.md} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename snippets/{arrayQuickSort.md => quickSort.md} (97%) diff --git a/snippets/arrayQuickSort.md b/snippets/quickSort.md similarity index 97% rename from snippets/arrayQuickSort.md rename to snippets/quickSort.md index cbc5f73c0..38e0e0fad 100644 --- a/snippets/arrayQuickSort.md +++ b/snippets/quickSort.md @@ -1,4 +1,4 @@ -### ArrayQuickSort +### quickSort QuickSort an Array (ascending sort by default). From 775944abc0c65ab175fed0e814a11979502d8474 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 28 Dec 2017 13:42:57 +0200 Subject: [PATCH 3/3] Update quickSort.md --- snippets/quickSort.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/snippets/quickSort.md b/snippets/quickSort.md index 38e0e0fad..c16faf2d0 100644 --- a/snippets/quickSort.md +++ b/snippets/quickSort.md @@ -2,7 +2,9 @@ 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. +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) =>