-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Bubble sort implementation (#46)
closes #20
- Loading branch information
Showing
7 changed files
with
157 additions
and
117 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Bubble Sort in JavaScript | ||
|
||
by [Nicholas C. Zakas](https://humanwhocodes.com) | ||
|
||
If you find this useful, please consider supporting my work with a [donation](https://humanwhocodes.com/donate). | ||
|
||
## Overview | ||
|
||
This is an implementation of the bubble sort algorithm in JavaScript. The function sorts an array in place. | ||
|
||
**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. | ||
|
||
## Usage | ||
|
||
Use CommonJS to get access to the `bubbleSort()` function: | ||
|
||
```js | ||
const { bubbleSort } = require("@humanwhocodes/bubble-sort"); | ||
|
||
const items = [1, 5, 2]; | ||
const result = bubbleSort(items); | ||
|
||
console.log(result); // [1, 2, 5] | ||
``` | ||
|
||
## Note on Code Style | ||
|
||
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. | ||
|
||
## Issues and Pull Requests | ||
|
||
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. | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* @fileoverview Bubble sort implementation in JavaScript | ||
*/ | ||
|
||
/** | ||
* Swaps two values in an array. | ||
* @param {Array} items The array containing the items. | ||
* @param {int} firstIndex Index of first item to swap. | ||
* @param {int} secondIndex Index of second item to swap. | ||
* @returns {void} | ||
*/ | ||
function swap(items, firstIndex, secondIndex){ | ||
var temp = items[firstIndex]; | ||
items[firstIndex] = items[secondIndex]; | ||
items[secondIndex] = temp; | ||
} | ||
|
||
/** | ||
* A bubble sort implementation in JavaScript. The array | ||
* is sorted in-place. | ||
* @param {Array} items An array of items to sort. | ||
* @return {Array} The sorted array. | ||
*/ | ||
exports.bubbleSort = (items) => { | ||
|
||
/* | ||
* The outer loop moves from the first item in the array to the last item | ||
* in the array. | ||
*/ | ||
for (let i = 0; i < items.length; i++){ | ||
|
||
/* | ||
* The inner loop also moves from the first item in the array towards | ||
* a stopping point. The `stop` value is the length of the array | ||
* minus the position of the outer loop minus one. The stop position | ||
* is used because items start by being sorted at the back of the | ||
* array and increasing towards the front of the array. The minus one | ||
* is necessary because we are comparing each item to the next | ||
* item, and the last item doesn't have a next item to compare to. | ||
*/ | ||
for (let j = 0, stop = items.length - i - 1; j < stop; j++){ | ||
|
||
/* | ||
* If the item at index `j` is greater than the item at index | ||
* `j + 1`, then swap the values. | ||
*/ | ||
if (items[j] > items[j + 1]){ | ||
swap(items, j, j + 1); | ||
} | ||
} | ||
} | ||
|
||
return items; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "@humanwhocodes/bubble-sort", | ||
"version": "2.0.0", | ||
"description": "A bubble sort implementation in JavaScript", | ||
"main": "bubble-sort.js", | ||
"scripts": { | ||
"test": "npx mocha ../../../../tests/algorithms/sorting/bubble-sort/bubble-sort.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/humanwhocodes/computer-science-in-javascript.git" | ||
}, | ||
"keywords": [ | ||
"sorting", | ||
"algorithm", | ||
"bubble sort" | ||
], | ||
"author": "Nicholas C. Zakas", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/humanwhocodes/computer-science-in-javascript/issues" | ||
}, | ||
"homepage": "https://github.com/humanwhocodes/computer-science-in-javascript#readme", | ||
"engines": { | ||
"node": ">=8.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @fileoverview Bubble Sort tests | ||
*/ | ||
/* global it, describe */ | ||
|
||
"use strict"; | ||
|
||
//----------------------------------------------------------------------------- | ||
// Requirements | ||
//----------------------------------------------------------------------------- | ||
|
||
const assert = require("chai").assert; | ||
const { bubbleSort } = require("../../../../src/algorithms/sorting/bubble-sort"); | ||
|
||
//----------------------------------------------------------------------------- | ||
// Tests | ||
//----------------------------------------------------------------------------- | ||
|
||
describe("bubbleSort()", () => { | ||
|
||
[ | ||
[], | ||
[1], | ||
[2, 1], | ||
[2, 1, 3], | ||
[32,4,5,7,9,4,1], | ||
[3,1,1,5,9,4,2, 5, 12, 45] | ||
].forEach(items => { | ||
|
||
it("should sort an array when the array has " + items.length + " item(s)", () => { | ||
const result = [...items].sort((a, b) => a - b); | ||
|
||
bubbleSort(items); | ||
assert.deepStrictEqual(items, result); | ||
}); | ||
|
||
}); | ||
|
||
}); |