chore: import upstream snapshot with attribution
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
This commit is contained in:
+134
@@ -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;
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user