mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat(curriculum): add merge sort workshop JSV9 (#66416)
Co-authored-by: Kolade <chrisjay967@gmail.com> Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com>
This commit is contained in:
@@ -5652,6 +5652,13 @@
|
||||
"In this workshop, you'll implement the binary search algorithm and return the path it took to find the target or return 'Value not found'."
|
||||
]
|
||||
},
|
||||
"workshop-merge-sort-js": {
|
||||
"title": "Implement the Merge Sort Algorithm",
|
||||
"intro": [
|
||||
"The merge sort algorithm is a sorting algorithm based on the divide and conquer principle.",
|
||||
"In this workshop, you'll implement the merge sort algorithm to sort a list of random numbers."
|
||||
]
|
||||
},
|
||||
"lab-bubble-sort-algorithm": {
|
||||
"title": "Implement the Bubble Sort Algorithm",
|
||||
"intro": [
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
---
|
||||
id: 69b2713ff6c21fed2d3ad88b
|
||||
title: Step 1
|
||||
challengeType: 1
|
||||
dashedName: step-1
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Merge sort is a sorting algorithm that uses the divide-and-conquer principle to sort collections of data. That is, it "divides" a collection into smaller sub-parts, and "conquers" the sub-parts by sorting them independently, then merges the sorted sub-parts.
|
||||
|
||||
Begin by creating a function called `mergeSort` that takes the parameter `array`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should declare a function named `mergeSort` with a single `array` parameter.
|
||||
|
||||
```js
|
||||
assert.isFunction(mergeSort);
|
||||
const params = __helpers.getFunctionParams(mergeSort.toString());
|
||||
assert.lengthOf(params, 1);
|
||||
assert.deepEqual(params[0].name, "array");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
---
|
||||
id: 69b2762b17d23fbee40a6c00
|
||||
title: Step 2
|
||||
challengeType: 1
|
||||
dashedName: step-2
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The merge sort algorithm performs three actions:
|
||||
|
||||
- Divide an unsorted sequence of items into sub-parts
|
||||
- Sort the items in the sub-parts
|
||||
- Merge the sorted sub-parts
|
||||
|
||||
The above happens recursively until the sub-parts are merged into the complete sorted sequence. Let's start by dividing the sequence.
|
||||
|
||||
First, inside the `mergeSort` function, declare a variable `middlePoint` using `const` and assign it `Math.floor(array.length / 2)` so you can split the sequence in half before recursing.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should declare a `middlePoint` variable and assign `Math.floor(array.length / 2)` to it.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/(const|let|var)\s+middlePoint\s*=\s*Math\.floor\(\s*array\.length\s*\/\s*2\s*\)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
```
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
---
|
||||
id: 69b278519ef46b2fd8bf1546
|
||||
title: Step 3
|
||||
challengeType: 1
|
||||
dashedName: step-3
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
You already have a `middlePoint` value. Use it to split the array into two halves and keep the left portion ready for sorting.
|
||||
|
||||
Use the slice syntax to extract the left half of `array` and assign it to a variable named `leftPart`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should declare a `leftPart` variable inside the `mergeSort` function.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/(let|const|var)\s+leftPart\s*=/
|
||||
);
|
||||
```
|
||||
|
||||
You should assign `array.slice(0, middlePoint)` to `leftPart`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/leftPart\s*=\s*array\.slice\(\s*0\s*,\s*middlePoint\s*\)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
```
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
---
|
||||
id: 69b278d82f0e514f32acb724
|
||||
title: Step 4
|
||||
challengeType: 1
|
||||
dashedName: step-4
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
After slicing the left half, you still need the right side to complete the merge step.
|
||||
|
||||
Use the slice syntax to extract the right half of `array` and assign it to a variable named `rightPart`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a `rightPart` variable inside the `mergeSort` function.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/(let|const|var)\s+rightPart\s*=/
|
||||
);
|
||||
```
|
||||
|
||||
You should use the slice syntax to extract the right half of the array and assign it to the `rightPart` variable.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/rightPart\s*=\s*array\.slice\(\s*middlePoint\s*\)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
```
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cef
|
||||
title: Step 5
|
||||
challengeType: 1
|
||||
dashedName: step-5
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Now that you've divided the `array` list into two separate lists, you'll keep dividing each list until every element stands alone in its own list. A list with a single number is always sorted.
|
||||
|
||||
To do that, recursively call `mergeSort` inside your function and pass `leftPart` as the argument to the call.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should call `mergeSort(leftPart)` inside the same `mergeSort` function.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/mergeSort\s*\(\s*leftPart\s*\)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
```
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cf0
|
||||
title: Step 6
|
||||
challengeType: 1
|
||||
dashedName: step-6
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
At the bottom of your function body, call the `mergeSort()` function again. This time, pass in `rightPart` as the argument to the function call.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should call `mergeSort()` with the argument `rightPart` at the bottom of your function body.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/mergeSort\s*\(\s*rightPart\s*\)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
```
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cf1
|
||||
title: Step 7
|
||||
challengeType: 1
|
||||
dashedName: step-7
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Now it's time to sort and merge the lists (`leftPart` and `rightPart`) into the original `array`.
|
||||
|
||||
You can do this by comparing elements on both lists, and merging the smaller element to the main list. You are going to do this comparison for all the indexes in `leftPart` and `rightPart`.
|
||||
|
||||
Create three variables: `leftArrayIndex`, `rightArrayIndex`, and `sortedIndex`, and set their values to `0`. These variables will help you keep track of each index during the sorting process.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should declare a `leftArrayIndex` variable initialized to `0`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/(let|const|var)\s+leftArrayIndex\s*=\s*0\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
You should declare a `rightArrayIndex` variable initialized to `0`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/(let|const|var)\s+rightArrayIndex\s*=\s*0\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
You should declare a `sortedIndex` variable initialized to `0`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/(let|const|var)\s+sortedIndex\s*=\s*0\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
```
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cf2
|
||||
title: Step 8
|
||||
challengeType: 1
|
||||
dashedName: step-8
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Next, you need to create a loop that continues as long as there are elements remaining in both `leftPart` and `rightPart`.
|
||||
|
||||
For that, create a `while` loop with two conditions: one that checks whether the `leftArrayIndex` is less than the length of `leftPart` and another condition that checks whether `rightArrayIndex` is less than the length of `rightPart`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should create a `while` loop that checks if `leftArrayIndex` is less than the length of `leftPart` and if `rightArrayIndex` is less than the length of `rightPart`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(code),
|
||||
/while\s*\(\s*leftArrayIndex\s*<\s*leftPart\.length\s*&&\s*rightArrayIndex\s*<\s*rightPart\.length\s*\)\s*\{/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
```
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cf3
|
||||
title: Step 9
|
||||
challengeType: 1
|
||||
dashedName: step-9
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Inside the loop, start comparing the current elements from each half so you can merge the smaller value first.
|
||||
|
||||
Add an `if` block that checks if the current element in `leftPart` is less than the current element in `rightPart`.
|
||||
# --hints--
|
||||
|
||||
The `if` condition should check if `leftPart[leftArrayIndex]` is less than `rightPart[rightArrayIndex]`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/if\s*\(\s*leftPart\s*\[\s*leftArrayIndex\s*\]\s*<\s*rightPart\s*\[\s*rightArrayIndex\s*\]\s*\)\s*\{/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
}
|
||||
```
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cf4
|
||||
title: Step 10
|
||||
challengeType: 1
|
||||
dashedName: step-10
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
When the `if` condition evaluates to `true`, it means that the element in the `leftPart` list is smaller than the element it is being compared to in the `rightPart` list.
|
||||
|
||||
In that case, you can put the element found at `leftArrayIndex` in `leftPart` within the sorted array.
|
||||
|
||||
Inside the `if` block, assign `leftPart[leftArrayIndex]` to `array[sortedIndex]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should assign `leftPart[leftArrayIndex]` to `array[sortedIndex]` inside the `if`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/array\s*\[\s*sortedIndex\s*\]\s*=\s*leftPart\s*\[\s*leftArrayIndex\s*\]\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cf5
|
||||
title: Step 11
|
||||
challengeType: 1
|
||||
dashedName: step-11
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
After assigning the element in `leftPart` to the sorted array, increment `leftArrayIndex` by `1`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should add to the current value of `leftArrayIndex` after inserting the element into the sorted array.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/leftArrayIndex\s*(\+\+|\+=\s*1)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cf6
|
||||
title: Step 12
|
||||
challengeType: 1
|
||||
dashedName: step-12
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In a previous step, you assigned the element in the `leftPart` to the `array` list because it was smaller. But this will not always be the case. In some comparison cases, the element on the right could be smaller.
|
||||
|
||||
Create an `else` clause to execute when the element in `leftPart` is not less than the element in `rightPart`.
|
||||
|
||||
Inside the `else` block, assign `rightPart[rightArrayIndex]` to `array[sortedIndex]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should create an `else` block that contains `array[sortedIndex] = rightPart[rightArrayIndex]`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/else\s*\{[\s\S]*array\s*\[\s*sortedIndex\s*\]\s*=\s*rightPart\s*\[\s*rightArrayIndex\s*\]\s*;?[\s\S]*\}/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
--fcc-editable-region--
|
||||
}
|
||||
--fcc-editable-region--
|
||||
}
|
||||
}
|
||||
```
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cf7
|
||||
title: Step 13
|
||||
challengeType: 1
|
||||
dashedName: step-13
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Still within the `else` block, increment `rightArrayIndex` by `1`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should increment `rightArrayIndex` after inserting the element into the sorted array.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/rightArrayIndex\s*(\+\+|\+=\s*1)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
} else {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cf8
|
||||
title: Step 14
|
||||
challengeType: 1
|
||||
dashedName: step-14
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The `if` and `else` statements you created in the previous steps will assign elements to the sorted array.
|
||||
|
||||
Each element assigned to the sorted array takes up an index in the list. So you have to move to the next index in the sorted array after each assignment.
|
||||
|
||||
Below the `if`/`else` block, but still within the `while` loop, increment `sortedIndex` by `1`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should increment the current value of `sortedIndex` by `1` after your `else` clause.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/while\s*\(\s*leftArrayIndex\s*<\s*leftPart\.length\s*&&\s*rightArrayIndex\s*<\s*rightPart\.length\s*\)\s*\{[\s\S]*sortedIndex\s*(\+\+|\+=\s*1)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
} else {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
}
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
}
|
||||
```
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cf9
|
||||
title: Step 15
|
||||
challengeType: 1
|
||||
dashedName: step-15
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The `while` loop you created compares one element from `leftPart` with another in `rightPart`, then adds the smaller element to the main `array` list.
|
||||
|
||||
It will continue this operation until there are no elements left to be compared. But `leftPart` may still have elements left while `rightPart` has none, and vice versa.
|
||||
|
||||
Create another `while` loop that runs when `leftArrayIndex` is less than the length of `leftPart`. In the next steps, you'll use it to copy the remaining elements in `leftPart` into the `array` list.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should create a `while` loop with the condition `leftArrayIndex < leftPart.length`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/while\s*\(\s*leftArrayIndex\s*<\s*leftPart\.length\s*\)\s*\{/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
} else {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
}
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
```
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cfa
|
||||
title: Step 16
|
||||
challengeType: 1
|
||||
dashedName: step-16
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Copy the remaining left values into `array` by assigning `leftPart[leftArrayIndex]` to `array[sortedIndex]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
The loop body should assign `leftPart[leftArrayIndex]` to `array[sortedIndex]`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/while\s*\(\s*leftArrayIndex\s*<\s*leftPart\.length\s*\)\s*\{[\s\S]*array\s*\[\s*sortedIndex\s*\]\s*=\s*leftPart\s*\[\s*leftArrayIndex\s*\]\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
} else {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
}
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (leftArrayIndex < leftPart.length) {
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
}
|
||||
```
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cfb
|
||||
title: Step 17
|
||||
challengeType: 1
|
||||
dashedName: step-17
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Still within the `while` loop, increment the value of `leftArrayIndex` by 1.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should increment the current value of `leftArrayIndex` by `1` after assigning the left part element to the sorted array.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/while\s*\(\s*leftArrayIndex\s*<\s*leftPart\.length\s*\)\s*\{[\s\S]*leftArrayIndex\s*(\+\+|\+=\s*1)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
} else {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
}
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (leftArrayIndex < leftPart.length) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
}
|
||||
```
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cfc
|
||||
title: Step 18
|
||||
challengeType: 1
|
||||
dashedName: step-18
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The last thing to do for the `while` loop is to move to the next index in the sorted array.
|
||||
|
||||
Add `1` to the value of `sortedIndex`.
|
||||
|
||||
Note that the increment should be done after assigning the left part element to the sorted array.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should increment the current value of `sortedIndex` by one within the `while` loop.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/while\s*\(\s*leftArrayIndex\s*<\s*leftPart\.length\s*\)\s*\{[\s\S]*sortedIndex\s*(\+\+|\+=\s*1)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
} else {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
}
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (leftArrayIndex < leftPart.length) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
}
|
||||
```
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cfd
|
||||
title: Step 19
|
||||
challengeType: 1
|
||||
dashedName: step-19
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Finally, you should copy any remaining values from the right half into `array`.
|
||||
|
||||
To do this, add a `while` loop that runs as long as `rightArrayIndex` is less than `rightPart.length`, assigns `rightPart[rightArrayIndex]` into `array[sortedIndex]`, and increments both `rightArrayIndex` and `sortedIndex`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should create a `while` loop with the condition `rightArrayIndex < rightPart.length`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/while\s*\(\s*rightArrayIndex\s*<\s*rightPart\.length\s*\)\s*\{/
|
||||
);
|
||||
```
|
||||
|
||||
The loop body should move `rightPart[rightArrayIndex]` into `array[sortedIndex]`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/while\s*\(\s*rightArrayIndex\s*<\s*rightPart\.length\s*\)\s*\{[\s\S]*array\s*\[\s*sortedIndex\s*\]\s*=\s*rightPart\s*\[\s*rightArrayIndex\s*\]\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
You should increment both `rightArrayIndex` and `sortedIndex` after the assignment.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/while\s*\(\s*rightArrayIndex\s*<\s*rightPart\.length\s*\)\s*\{[\s\S]*rightArrayIndex\s*(\+\+|\+=\s*1)\s*;?[\s\S]*sortedIndex\s*(\+\+|\+=\s*1)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
} else {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
}
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (leftArrayIndex < leftPart.length) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
}
|
||||
```
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cfe
|
||||
title: Step 20
|
||||
challengeType: 1
|
||||
dashedName: step-20
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Before testing the `mergeSort()` function, you need to create a base case that stops the function execution when the length of `array` is less than or equal to `1`.
|
||||
|
||||
This base case will stop the recursion call. Without it, the merge sort operation would continue to run even when the list has been sorted or has no elements in it.
|
||||
|
||||
Right after the function declaration, create an `if` statement that checks if `array.length` is less than or equal to 1. Within the body of the `if` statement, add a `return` statement to stop the execution of the function.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should create an `if` statement with the condition `array.length <= 1`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/if\s*\(\s*array\.length\s*<=\s*1\s*\)\s*\{/
|
||||
);
|
||||
```
|
||||
|
||||
You should have a `return` statement in the body of the `if` statement.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(mergeSort.toString()),
|
||||
/if\s*\(\s*array\.length\s*<=\s*1\s*\)\s*\{[\s\S]*return\s*;?[\s\S]*\}/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
} else {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
}
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (leftArrayIndex < leftPart.length) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (rightArrayIndex < rightPart.length) {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
}
|
||||
```
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163cff
|
||||
title: Step 21
|
||||
challengeType: 1
|
||||
dashedName: step-21
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Outside the function definition, create an array `numbers` with the following values: `[4, 10, 6, 14, 2, 1, 8, 5]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should create an array called `numbers` with these values: `[4, 10, 6, 14, 2, 1, 8, 5]`
|
||||
|
||||
```js
|
||||
const cleaned = __helpers.removeJSComments(code);
|
||||
assert.match(
|
||||
cleaned,
|
||||
/(const|let|var)\s+numbers\s*=\s*\[\s*4\s*,\s*10\s*,\s*6\s*,\s*14\s*,\s*2\s*,\s*1\s*,\s*8\s*,\s*5\s*\]\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
if (array.length <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
} else {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
}
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (leftArrayIndex < leftPart.length) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (rightArrayIndex < rightPart.length) {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
}
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163d00
|
||||
title: Step 22
|
||||
challengeType: 1
|
||||
dashedName: step-22
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Log the string `'Unsorted array: '` to the console.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should log the specified string by using `console.log('Unsorted array: ')`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(code),
|
||||
/console\.log\s*\(\s*['"]Unsorted array:\s*['"]\s*\)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
if (array.length <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
} else {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
}
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (leftArrayIndex < leftPart.length) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (rightArrayIndex < rightPart.length) {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
}
|
||||
|
||||
const numbers = [4, 10, 6, 14, 2, 1, 8, 5];
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163d01
|
||||
title: Step 23
|
||||
challengeType: 1
|
||||
dashedName: step-23
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
After printing the string, log the `numbers` array itself so the unsorted list appears next.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should call `console.log(numbers);` after the `'Unsorted array: '` message.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(code),
|
||||
/console\.log\s*\(\s*['"]Unsorted array:\s*['"]\s*\)\s*;?\s*console\.log\s*\(\s*numbers\s*\)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
if (array.length <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
} else {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
}
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (leftArrayIndex < leftPart.length) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (rightArrayIndex < rightPart.length) {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
}
|
||||
|
||||
const numbers = [4, 10, 6, 14, 2, 1, 8, 5];
|
||||
console.log('Unsorted array: ');
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163d02
|
||||
title: Step 24
|
||||
challengeType: 1
|
||||
dashedName: step-24
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
After logging the unsorted array, call the `mergeSort` function and pass in the `numbers` array as an argument.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should call `mergeSort(numbers)` after logging the unsorted array.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(code),
|
||||
/console\.log\s*\(\s*['"]Unsorted array:\s*['"]\s*\)\s*;?\s*console\.log\s*\(\s*numbers\s*\)\s*;?\s*mergeSort\s*\(\s*numbers\s*\)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
if (array.length <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
} else {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
}
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (leftArrayIndex < leftPart.length) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (rightArrayIndex < rightPart.length) {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
}
|
||||
|
||||
const numbers = [4, 10, 6, 14, 2, 1, 8, 5];
|
||||
console.log('Unsorted array: ');
|
||||
console.log(numbers);
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
---
|
||||
id: 69b279bae0f20eda63163d03
|
||||
title: Step 25
|
||||
challengeType: 1
|
||||
dashedName: step-25
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Now that `numbers` is sorted, log `'Sorted array: '` to the console followed by the `numbers` array.
|
||||
|
||||
With that, the merge sort algorithm is complete.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should log `'Sorted array: '` to the console.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(code),
|
||||
/console\.log\s*\(\s*['"]Sorted array:\s*['"]\s*\)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
You should log the `numbers` array to the console after logging `'Sorted array: '`.
|
||||
|
||||
```js
|
||||
assert.match(
|
||||
__helpers.removeJSComments(code),
|
||||
/mergeSort\s*\(\s*numbers\s*\)\s*;?\s*console\.log\s*\(\s*['"]Sorted array:\s*['"]\s*\)\s*;?\s*console\.log\s*\(\s*numbers\s*\)\s*;?/
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
if (array.length <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
} else {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
}
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (leftArrayIndex < leftPart.length) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (rightArrayIndex < rightPart.length) {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
}
|
||||
const numbers = [4, 10, 6, 14, 2, 1, 8, 5];
|
||||
console.log('Unsorted array: ');
|
||||
console.log(numbers);
|
||||
mergeSort(numbers);
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
if (array.length <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
const middlePoint = Math.floor(array.length / 2);
|
||||
const leftPart = array.slice(0, middlePoint);
|
||||
const rightPart = array.slice(middlePoint);
|
||||
|
||||
mergeSort(leftPart);
|
||||
mergeSort(rightPart);
|
||||
|
||||
let leftArrayIndex = 0;
|
||||
let rightArrayIndex = 0;
|
||||
let sortedIndex = 0;
|
||||
|
||||
while (leftArrayIndex < leftPart.length && rightArrayIndex < rightPart.length) {
|
||||
if (leftPart[leftArrayIndex] < rightPart[rightArrayIndex]) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
} else {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
}
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (leftArrayIndex < leftPart.length) {
|
||||
array[sortedIndex] = leftPart[leftArrayIndex];
|
||||
leftArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
|
||||
while (rightArrayIndex < rightPart.length) {
|
||||
array[sortedIndex] = rightPart[rightArrayIndex];
|
||||
rightArrayIndex += 1;
|
||||
sortedIndex += 1;
|
||||
}
|
||||
}
|
||||
|
||||
const numbers = [4, 10, 6, 14, 2, 1, 8, 5];
|
||||
console.log('Unsorted array:');
|
||||
console.log(numbers);
|
||||
mergeSort(numbers);
|
||||
console.log('Sorted array:');
|
||||
console.log(numbers);
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"isUpcomingChange": true,
|
||||
"dashedName": "workshop-merge-sort-js",
|
||||
"helpCategory": "JavaScript",
|
||||
"blockLayout": "challenge-grid",
|
||||
"challengeOrder": [
|
||||
{ "id": "69b2713ff6c21fed2d3ad88b", "title": "Step 1" },
|
||||
{ "id": "69b2762b17d23fbee40a6c00", "title": "Step 2" },
|
||||
{ "id": "69b278519ef46b2fd8bf1546", "title": "Step 3" },
|
||||
{ "id": "69b278d82f0e514f32acb724", "title": "Step 4" },
|
||||
{ "id": "69b279bae0f20eda63163cef", "title": "Step 5" },
|
||||
{ "id": "69b279bae0f20eda63163cf0", "title": "Step 6" },
|
||||
{ "id": "69b279bae0f20eda63163cf1", "title": "Step 7" },
|
||||
{ "id": "69b279bae0f20eda63163cf2", "title": "Step 8" },
|
||||
{ "id": "69b279bae0f20eda63163cf3", "title": "Step 9" },
|
||||
{ "id": "69b279bae0f20eda63163cf4", "title": "Step 10" },
|
||||
{ "id": "69b279bae0f20eda63163cf5", "title": "Step 11" },
|
||||
{ "id": "69b279bae0f20eda63163cf6", "title": "Step 12" },
|
||||
{ "id": "69b279bae0f20eda63163cf7", "title": "Step 13" },
|
||||
{ "id": "69b279bae0f20eda63163cf8", "title": "Step 14" },
|
||||
{ "id": "69b279bae0f20eda63163cf9", "title": "Step 15" },
|
||||
{ "id": "69b279bae0f20eda63163cfa", "title": "Step 16" },
|
||||
{ "id": "69b279bae0f20eda63163cfb", "title": "Step 17" },
|
||||
{ "id": "69b279bae0f20eda63163cfc", "title": "Step 18" },
|
||||
{ "id": "69b279bae0f20eda63163cfd", "title": "Step 19" },
|
||||
{ "id": "69b279bae0f20eda63163cfe", "title": "Step 20" },
|
||||
{ "id": "69b279bae0f20eda63163cff", "title": "Step 21" },
|
||||
{ "id": "69b279bae0f20eda63163d00", "title": "Step 22" },
|
||||
{ "id": "69b279bae0f20eda63163d01", "title": "Step 23" },
|
||||
{ "id": "69b279bae0f20eda63163d02", "title": "Step 24" },
|
||||
{ "id": "69b279bae0f20eda63163d03", "title": "Step 25" }
|
||||
],
|
||||
"blockLabel": "workshop",
|
||||
"usesMultifileEditor": true,
|
||||
"hasEditableBoundaries": true
|
||||
}
|
||||
@@ -316,6 +316,7 @@
|
||||
"blocks": [
|
||||
"lecture-introduction-to-common-searching-and-sorting-algorithms",
|
||||
"workshop-binary-search-js",
|
||||
"workshop-merge-sort-js",
|
||||
"lab-bubble-sort-algorithm",
|
||||
"lab-selection-sort-js",
|
||||
"lab-insertion-sort",
|
||||
|
||||
Reference in New Issue
Block a user