Coder Social home page Coder Social logo

bubblesort's Introduction

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

bubblesort's People

Contributors

addyosmani avatar arthurvr avatar coderaiser avatar jasonsjones avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.