mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-16 16:00:45 -04:00
feat(curriculum): convert bubble sort challenge into js lab (#65708)
This commit is contained in:
@@ -3158,6 +3158,12 @@
|
||||
"These lessons cover algorithm implementations, time and space complexity analysis, and the divide and conquer programming paradigm."
|
||||
]
|
||||
},
|
||||
"lab-bubble-sort-algorithm": {
|
||||
"title": "Implement the Bubble Sort Algorithm",
|
||||
"intro": [
|
||||
"In this lab, you will implement the bubble sort algorithm to sort an array of integers in ascending order."
|
||||
]
|
||||
},
|
||||
"lecture-understanding-graphs-and-trees-js": {
|
||||
"title": "Understanding Graphs and Trees",
|
||||
"intro": [
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
---
|
||||
id: 698367b9d1c6914a22095aec
|
||||
title: Implement the Bubble Sort Algorithm
|
||||
challengeType: 26
|
||||
dashedName: implement-the-bubble-sort-algorithm
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
For this lab, you will implement the bubble sort algorithm. It starts at the beginning of an unsorted array and "bubbles up" unsorted values towards the end, iterating through the array until it is completely sorted. It does this by comparing adjacent items and swapping them if they are out of order. The method continues looping through the array until no swaps occur at which point the array is sorted.
|
||||
|
||||
This method requires multiple iterations through the array and for average and worst cases has quadratic time complexity. While simple, it is usually impractical in most situations.
|
||||
|
||||
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
|
||||
|
||||
**User Stories:**
|
||||
|
||||
1. Write a function `bubbleSort` which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a function named `bubbleSort`.
|
||||
|
||||
```js
|
||||
assert.isFunction(bubbleSort);
|
||||
```
|
||||
|
||||
`bubbleSort` 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.isTrue(
|
||||
isSorted(
|
||||
bubbleSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
])
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
`bubbleSort([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(
|
||||
bubbleSort([
|
||||
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]
|
||||
);
|
||||
```
|
||||
|
||||
`bubbleSort` 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 {
|
||||
bubbleSort([0, 1]);
|
||||
} finally {
|
||||
Array.prototype.sort = temp;
|
||||
}
|
||||
return sortUsed;
|
||||
}
|
||||
assert.isFalse(isBuiltInSortUsed());
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function bubbleSort(array) {
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
let swapped = false;
|
||||
for (let j = 1; j < array.length; j++) {
|
||||
if (array[j - 1] > array[j]) {
|
||||
let temp = array[j-1];
|
||||
array[j-1] = array[j];
|
||||
array[j] = temp;
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
if (swapped === false) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
```
|
||||
15
curriculum/structure/blocks/lab-bubble-sort-algorithm.json
Normal file
15
curriculum/structure/blocks/lab-bubble-sort-algorithm.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "Implement the Bubble Sort Algorithm",
|
||||
"isUpcomingChange": true,
|
||||
"dashedName": "lab-bubble-sort-algorithm",
|
||||
"helpCategory": "JavaScript",
|
||||
"blockLayout": "link",
|
||||
"challengeOrder": [
|
||||
{
|
||||
"id": "698367b9d1c6914a22095aec",
|
||||
"title": "Implement the Bubble Sort Algorithm"
|
||||
}
|
||||
],
|
||||
"blockLabel": "lab",
|
||||
"usesMultifileEditor": true
|
||||
}
|
||||
@@ -300,7 +300,8 @@
|
||||
"dashedName": "algorithms",
|
||||
"comingSoon": true,
|
||||
"blocks": [
|
||||
"lecture-introduction-to-common-searching-and-sorting-algorithms"
|
||||
"lecture-introduction-to-common-searching-and-sorting-algorithms",
|
||||
"lab-bubble-sort-algorithm"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user