diff --git a/client/i18n/locales/english/intro.json b/client/i18n/locales/english/intro.json index 3447f382be3..e1fb12a13fd 100644 --- a/client/i18n/locales/english/intro.json +++ b/client/i18n/locales/english/intro.json @@ -3225,6 +3225,12 @@ "In this lab, you will implement the bubble sort algorithm to sort an array of integers in ascending order." ] }, + "lab-quicksort-js": { + "title": "Implement the Quicksort Algorithm", + "intro": [ + "In this lab you will implement the quicksort algorithm to sort an array of integers." + ] + }, "lecture-understanding-graphs-and-trees-js": { "title": "Understanding Graphs and Trees", "intro": [ diff --git a/curriculum/challenges/english/blocks/lab-quicksort-js/587d825a367417b2b2512c89.md b/curriculum/challenges/english/blocks/lab-quicksort-js/587d825a367417b2b2512c89.md new file mode 100644 index 00000000000..b31b72fa4a7 --- /dev/null +++ b/curriculum/challenges/english/blocks/lab-quicksort-js/587d825a367417b2b2512c89.md @@ -0,0 +1,103 @@ +--- +id: 587d825a367417b2b2512c89 +title: Implement the Quicksort Algorithm +challengeType: 26 +dashedName: implement-the-quicksort-algorithm +--- + +# --description-- + +In this lab you will implement the sorting algorithm: quicksort. + +Quicksort is an efficient, recursive divide-and-conquer approach to sorting an array. In this method, a pivot value is chosen in the original array. The array is then partitioned into two subarrays of values less than and greater than the pivot value. Then the result of recursively calling the quicksort algorithm on both sub-arrays is combined. This continues until the base case of an empty or single-item array is reached, which is returned. The unwinding of the recursive calls gives back the sorted array. + +While the choice of the pivot value is important, any pivot will do for our purposes here. For simplicity, the first or last element could be used. + +Quicksort is a very efficient sorting method, providing *O(nlog(n))* performance on average. It is also relatively easy to implement. These attributes make it a popular and useful sorting method. + +**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab. + +**User Stories:** + +1. You should have a `quicksort` function that takes an array of integers as input. +1. The `quicksort` function should return an array that contains the same integers as the input, but in order from lowest to highest. +1. The `quicksort` function should recursively call itself to sort the array. + +# --hints-- + +You should have a `quicksort` function. + +```js +assert.isFunction(quicksort); +``` + +`quicksort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92])` should return an array that is unchanged except for order. + +```js +assert.sameMembers( + quicksort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]), + [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92] +); +``` + +`quicksort` should return a sorted array (least to greatest). + +```js +assert.sameOrderedMembers( + quicksort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]), + [1, 1, 2, 2, 4, 8, 32, 43, 43, 55, 63, 92, 123, 123, 234, 345, 5643] +); +``` + +`quicksort` should not use the built-in `.sort()` method. + +```js +function isBuiltInSortUsed(){ + let sortUsed = false; + const temp = Array.prototype.sort; + Array.prototype.sort = () => sortUsed = true; + try { + quicksort([0, 1]); + } finally { + Array.prototype.sort = temp; + } + return sortUsed; +} +assert.isFalse(isBuiltInSortUsed()); +``` + +# --seed-- + +## --seed-contents-- + +```js + +``` + +# --solutions-- + +```js +function quicksort(array) { + if (array.length === 0) { + return []; + } else { + const pivotValue = array[0]; + + // Sort elements into three piles + let lesser = []; + let equal = []; + let greater = []; + for (let e of array) { + if (e < pivotValue) { + lesser.push(e); + } else if (e > pivotValue) { + greater.push(e); + } else { + equal.push(e); + } + } + + return [...quicksort(lesser), ...equal, ...quicksort(greater)]; + } +} +``` diff --git a/curriculum/structure/blocks/lab-quicksort-js.json b/curriculum/structure/blocks/lab-quicksort-js.json new file mode 100644 index 00000000000..1ec901d37e9 --- /dev/null +++ b/curriculum/structure/blocks/lab-quicksort-js.json @@ -0,0 +1,15 @@ +{ + "name": "Implement the Quicksort Algorithm", + "isUpcomingChange": true, + "dashedName": "lab-quicksort-js", + "helpCategory": "JavaScript", + "blockLayout": "link", + "challengeOrder": [ + { + "id": "587d825a367417b2b2512c89", + "title": "Implement the Quicksort Algorithm" + } + ], + "blockLabel": "lab", + "usesMultifileEditor": true +} diff --git a/curriculum/structure/superblocks/javascript-v9.json b/curriculum/structure/superblocks/javascript-v9.json index ecbb166780e..dfa529018fe 100644 --- a/curriculum/structure/superblocks/javascript-v9.json +++ b/curriculum/structure/superblocks/javascript-v9.json @@ -301,7 +301,8 @@ "comingSoon": true, "blocks": [ "lecture-introduction-to-common-searching-and-sorting-algorithms", - "lab-bubble-sort-algorithm" + "lab-bubble-sort-algorithm", + "lab-quicksort-js" ] }, {