feat(curriculum): add quicksort lab to frontend cert (#56009)

Co-authored-by: Sem Bauke <sem@freecodecamp.org>
Co-authored-by: Dario-DC <105294544+Dario-DC@users.noreply.github.com>
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
pull/56027/head
Ilenia 2024-09-06 09:47:42 +02:00 committed by GitHub
parent 2910ff01df
commit 8d0550d76c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 168 additions and 1 deletions

View File

@ -2056,7 +2056,12 @@
"yshh": { "title": "237", "intro": [] }, "yshh": { "title": "237", "intro": [] },
"wyxz": { "title": "238", "intro": [] }, "wyxz": { "title": "238", "intro": [] },
"dtfv": { "title": "239", "intro": [] }, "dtfv": { "title": "239", "intro": [] },
"miza": { "title": "240", "intro": [] }, "lab-quicksort-algorithm": {
"title": "Build the Quicksort Algorithm",
"intro": [
"For this lab, you will implement the Quicksort algorithm using JavaScript."
]
},
"manv": { "title": "241", "intro": [] }, "manv": { "title": "241", "intro": [] },
"bnvw": { "title": "242", "intro": [] }, "bnvw": { "title": "242", "intro": [] },
"xkhk": { "title": "243", "intro": [] }, "xkhk": { "title": "243", "intro": [] },

View File

@ -0,0 +1,9 @@
---
title: Introduction to the Build the Quicksort Algorithm
block: lab-quicksort-algorithm
superBlock: front-end-development
---
## Introduction to the Build the Quicksort Algorithm
For this lab, you will implement the Quicksort algorithm using JavaScript.

View File

@ -0,0 +1,11 @@
{
"name": "Build the Quicksort Algorithm",
"isUpcomingChange": true,
"usesMultifileEditor": true,
"dashedName": "lab-quicksort-algorithm",
"order": 240,
"superBlock": "front-end-development",
"challengeOrder": [{ "id": "66d97c3be4030e8f41a92083", "title": "Build the Quicksort Algorithm" }],
"helpCategory": "JavaScript",
"blockType": "lab"
}

View File

@ -0,0 +1,142 @@
---
id: 66d97c3be4030e8f41a92083
title: Build the Quicksort Algorithm
challengeType: 14
dashedName: build-the-quicksort-algorithm
---
# --description--
Quicksort is an algorithm that uses the divide and conquer approach to sort arrays.
It can be implemented as follows:
- Choose a pivot value among the elements of the input array (use the first or the last element of the array).
- Partition the input array into three subarrays containing elements greater than, lesser than, and equal to the pivot value.
- Recursively call `quicksort` to sort the subarrays.
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
**User Stories:**
1. You should define a function named `quicksort` to implement the quicksort algorithm.
1. The `quicksort` function should take an array of integers as input and return an array of these integers in sorted order from least to greatest.
# --hints--
`quicksort` should be a function.
```js
assert.isFunction(quicksort);
```
`quicksort` should return a sorted array (least to greatest).
```js
function isSorted(a){
for(let i = 0; i < a.length - 1; i++)
if(a[i] > a[i + 1])
return false;
return true;
}
assert(
isSorted(
quicksort([
1,
4,
2,
8,
345,
123,
43,
32,
5643,
63,
123,
43,
2,
55,
1,
234,
92
])
)
);
```
`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 not use the built-in `.sort()` method.
```js
function isBuiltInSortUsed(){
let sortUsed = false;
const temp = Array.prototype.sort;
Array.prototype.sort = () => sortUsed = true;
quicksort([0, 1]);
Array.prototype.sort = temp;
return !sortUsed;
}
assert(isBuiltInSortUsed());
```
# --seed--
## --seed-contents--
```js
```
# --solutions--
```js
const quicksort = (array) => {
if (array.length === 0) {
return [];
}
const pivot = array[0];
let lesser = [];
let equal = [];
let greater = [];
for (let i of array) {
if (i < pivot) {
lesser.push(i);
} else if (i > pivot) {
greater.push(i);
} else {
equal.push(i);
}
}
return [...quicksort(lesser), ...equal, ...quicksort(greater)];
}
```