bubblesort 0,0 editorconfig travis-ci grunt npm

Bubble Sort implementation with O(n^2) complexity.

2 years after

NPM version Build Status Dependency Status

Bubble Sort

Bubble Sort implementation wth O(n^2) complexity based on JavaScript Algorithms.

Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.

Install

$ npm install --save bubblesort

Usage

var bubblesort = require('bubblesort');

// Ascending order

bubblesort([3,1,4,1,5,9,2,6,5,4]);
// => [1,1,2,3,4,4,5,5,6,9]

bubblesort([9,2,8,6,1,3]);
// => [1,2,3,6,8,9]

bubblesort([5,2,4,6,1,3]);
// => [1,2,3,4,5,6]

// Descending order

function comparator(a, b) { return b - a; }

bubblesort([5,2,2,6,1,3], comparator);
// => [6,5,3,2,2,1]

bubblesort([0,0,0,0,0,-1], comparator);
// => [0,0,0,0,0,-1]

// Ascending sort arrays of objects

var people = [
  {name: 'Sindre', age: 30, place: 2},
  {name: 'Passy', age: 25, place: 1},
  {name: 'Stephen', age: 40, place: 4},
  {name: 'Matt', age: 35, place: 3}
];

function comparePeople(a, b) { return a.age - b.age };

bubblesort(people, comparePeople);
/*
=> [
  {"name": 'Passy', "age": 25, "place": 1},
  {"name": 'Sindre', "age": 30, "place": 2},
  {"name": 'Matt', "age": 35, "place": 3},
  {"name": 'Stephen', "age": 40, "place": 4}
]
*/

License

MIT © Addy Osmani

Related Repositories

scalacaster

scalacaster

Purely Functional Algorithms and Data Structures in Scala ...

Python

Python

All Algorithms implemented in Python ...

Celero

Celero

C++ Benchmark Authoring Library/Framework ...

Sorts

Sorts

The algorithm of sort.Personal site: ...

awesome-stars

awesome-stars

:book: My own stars :star: ...


Top Contributors

addyosmani nperez0111 arthurvr jasonsjones coderaiser

Dependencies

package version
dev grunt-cli ^0.1.13
grunt-contrib-jshint ^0.10.0
grunt-contrib-nodeunit ^0.4.1
grunt-contrib-watch ^0.6.1
load-grunt-tasks ^1.0.0
time-grunt ^1.0.0
grunt-mocha-cli ^1.11.0
jshint-stylish ^1.0.0

Releases

-   v1.0.1 zip tar
-   v1.0.0 zip tar