From b321f075fd1c59f57b5cb48311a62ea0ea43871c Mon Sep 17 00:00:00 2001 From: Dario <105294544+Dario-DC@users.noreply.github.com> Date: Fri, 6 Feb 2026 20:04:27 +0100 Subject: [PATCH] feat(curriculum): convert bubble sort challenge into js lab (#65708) --- client/i18n/locales/english/intro.json | 6 + .../698367b9d1c6914a22095aec.md | 134 ++++++++++++++++++ .../blocks/lab-bubble-sort-algorithm.json | 15 ++ .../structure/superblocks/javascript-v9.json | 3 +- 4 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 curriculum/challenges/english/blocks/lab-bubble-sort-algorithm/698367b9d1c6914a22095aec.md create mode 100644 curriculum/structure/blocks/lab-bubble-sort-algorithm.json diff --git a/client/i18n/locales/english/intro.json b/client/i18n/locales/english/intro.json index ea52c4f4ce7..b507eed6669 100644 --- a/client/i18n/locales/english/intro.json +++ b/client/i18n/locales/english/intro.json @@ -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": [ diff --git a/curriculum/challenges/english/blocks/lab-bubble-sort-algorithm/698367b9d1c6914a22095aec.md b/curriculum/challenges/english/blocks/lab-bubble-sort-algorithm/698367b9d1c6914a22095aec.md new file mode 100644 index 00000000000..ca5eb973a80 --- /dev/null +++ b/curriculum/challenges/english/blocks/lab-bubble-sort-algorithm/698367b9d1c6914a22095aec.md @@ -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; +} +``` diff --git a/curriculum/structure/blocks/lab-bubble-sort-algorithm.json b/curriculum/structure/blocks/lab-bubble-sort-algorithm.json new file mode 100644 index 00000000000..63294f76299 --- /dev/null +++ b/curriculum/structure/blocks/lab-bubble-sort-algorithm.json @@ -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 +} diff --git a/curriculum/structure/superblocks/javascript-v9.json b/curriculum/structure/superblocks/javascript-v9.json index a1f08dd78b6..ecbb166780e 100644 --- a/curriculum/structure/superblocks/javascript-v9.json +++ b/curriculum/structure/superblocks/javascript-v9.json @@ -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" ] }, {