Skip to content

Commit 7e95eca

Browse files
authored
New: Bubble sort implementation (#46)
closes #20
1 parent 8311318 commit 7e95eca

File tree

7 files changed

+157
-117
lines changed

7 files changed

+157
-117
lines changed

algorithms/sorting/bubble-sort/bubble-sort-2.js

Lines changed: 0 additions & 58 deletions
This file was deleted.

algorithms/sorting/bubble-sort/bubble-sort.js

Lines changed: 0 additions & 58 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"main": "README.md",
77
"scripts": {
88
"lint": "eslint src/ tests/",
9-
"test": "npm run lint && mocha tests/*/*/*.js"
9+
"test": "npm run lint && mocha tests/**/*.js"
1010
},
1111
"repository": {
1212
"type": "git",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Bubble Sort in JavaScript
2+
3+
by [Nicholas C. Zakas](https://humanwhocodes.com)
4+
5+
If you find this useful, please consider supporting my work with a [donation](https://humanwhocodes.com/donate).
6+
7+
## Overview
8+
9+
This is an implementation of the bubble sort algorithm in JavaScript. The function sorts an array in place.
10+
11+
**Note:** You should always use the builtin `sort()` method on arrays in your code because it is already optimized for production use. This implementation should be used for learning purposes only.
12+
13+
## Usage
14+
15+
Use CommonJS to get access to the `bubbleSort()` function:
16+
17+
```js
18+
const { bubbleSort } = require("@humanwhocodes/bubble-sort");
19+
20+
const items = [1, 5, 2];
21+
const result = bubbleSort(items);
22+
23+
console.log(result); // [1, 2, 5]
24+
```
25+
26+
## Note on Code Style
27+
28+
You may find the code style of this module to be overly verbose with a lot of comments. That is intentional, as the primary use of this module is intended to be for educational purposes. There are frequently more concise ways of implementing the details of this class, but the more concise ways are difficult for newcomers who are unfamiliar with linked lists as a concept or JavaScript as a whole.
29+
30+
## Issues and Pull Requests
31+
32+
As this is part of series of tutorials I'm writing, only bug fixes will be accepted. No new functionality will be added to this module.
33+
34+
## License
35+
36+
MIT
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @fileoverview Bubble sort implementation in JavaScript
3+
*/
4+
5+
/**
6+
* Swaps two values in an array.
7+
* @param {Array} items The array containing the items.
8+
* @param {int} firstIndex Index of first item to swap.
9+
* @param {int} secondIndex Index of second item to swap.
10+
* @returns {void}
11+
*/
12+
function swap(items, firstIndex, secondIndex){
13+
var temp = items[firstIndex];
14+
items[firstIndex] = items[secondIndex];
15+
items[secondIndex] = temp;
16+
}
17+
18+
/**
19+
* A bubble sort implementation in JavaScript. The array
20+
* is sorted in-place.
21+
* @param {Array} items An array of items to sort.
22+
* @return {Array} The sorted array.
23+
*/
24+
exports.bubbleSort = (items) => {
25+
26+
/*
27+
* The outer loop moves from the first item in the array to the last item
28+
* in the array.
29+
*/
30+
for (let i = 0; i < items.length; i++){
31+
32+
/*
33+
* The inner loop also moves from the first item in the array towards
34+
* a stopping point. The `stop` value is the length of the array
35+
* minus the position of the outer loop minus one. The stop position
36+
* is used because items start by being sorted at the back of the
37+
* array and increasing towards the front of the array. The minus one
38+
* is necessary because we are comparing each item to the next
39+
* item, and the last item doesn't have a next item to compare to.
40+
*/
41+
for (let j = 0, stop = items.length - i - 1; j < stop; j++){
42+
43+
/*
44+
* If the item at index `j` is greater than the item at index
45+
* `j + 1`, then swap the values.
46+
*/
47+
if (items[j] > items[j + 1]){
48+
swap(items, j, j + 1);
49+
}
50+
}
51+
}
52+
53+
return items;
54+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "@humanwhocodes/bubble-sort",
3+
"version": "2.0.0",
4+
"description": "A bubble sort implementation in JavaScript",
5+
"main": "bubble-sort.js",
6+
"scripts": {
7+
"test": "npx mocha ../../../../tests/algorithms/sorting/bubble-sort/bubble-sort.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/humanwhocodes/computer-science-in-javascript.git"
12+
},
13+
"keywords": [
14+
"sorting",
15+
"algorithm",
16+
"bubble sort"
17+
],
18+
"author": "Nicholas C. Zakas",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/humanwhocodes/computer-science-in-javascript/issues"
22+
},
23+
"homepage": "https://github.com/humanwhocodes/computer-science-in-javascript#readme",
24+
"engines": {
25+
"node": ">=8.0.0"
26+
}
27+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @fileoverview Bubble Sort tests
3+
*/
4+
/* global it, describe */
5+
6+
"use strict";
7+
8+
//-----------------------------------------------------------------------------
9+
// Requirements
10+
//-----------------------------------------------------------------------------
11+
12+
const assert = require("chai").assert;
13+
const { bubbleSort } = require("../../../../src/algorithms/sorting/bubble-sort");
14+
15+
//-----------------------------------------------------------------------------
16+
// Tests
17+
//-----------------------------------------------------------------------------
18+
19+
describe("bubbleSort()", () => {
20+
21+
[
22+
[],
23+
[1],
24+
[2, 1],
25+
[2, 1, 3],
26+
[32,4,5,7,9,4,1],
27+
[3,1,1,5,9,4,2, 5, 12, 45]
28+
].forEach(items => {
29+
30+
it("should sort an array when the array has " + items.length + " item(s)", () => {
31+
const result = [...items].sort((a, b) => a - b);
32+
33+
bubbleSort(items);
34+
assert.deepStrictEqual(items, result);
35+
});
36+
37+
});
38+
39+
});

0 commit comments

Comments
 (0)