Skip to content

Commit

Permalink
New: Bubble sort implementation (#46)
Browse files Browse the repository at this point in the history
closes #20
  • Loading branch information
nzakas authored Dec 27, 2018
1 parent 8311318 commit 7e95eca
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 117 deletions.
58 changes: 0 additions & 58 deletions algorithms/sorting/bubble-sort/bubble-sort-2.js

This file was deleted.

58 changes: 0 additions & 58 deletions algorithms/sorting/bubble-sort/bubble-sort.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"main": "README.md",
"scripts": {
"lint": "eslint src/ tests/",
"test": "npm run lint && mocha tests/*/*/*.js"
"test": "npm run lint && mocha tests/**/*.js"
},
"repository": {
"type": "git",
Expand Down
36 changes: 36 additions & 0 deletions src/algorithms/sorting/bubble-sort/README.md
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
54 changes: 54 additions & 0 deletions src/algorithms/sorting/bubble-sort/bubble-sort.js
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;
};
27 changes: 27 additions & 0 deletions src/algorithms/sorting/bubble-sort/package.json
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"
}
}
39 changes: 39 additions & 0 deletions tests/algorithms/sorting/bubble-sort/bubble-sort.js
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);
});

});

});

0 comments on commit 7e95eca

Please sign in to comment.