Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ eg:
test.setData(false) // [5, 6, 6, 8, 3, 2, 2]
test.setData() // [1, 7, 6, 8, 3, 2, 5]
```
### List of sorting algorithms
* Bubble sort
* Counting sort
* Insertion sort
* merge sort
* quick sort
* selection sort


## For Geeks :P
### How to Test
Expand Down
116 changes: 55 additions & 61 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,63 +1,57 @@
{
"name": "sorting-javascript",
"version": "1.0.4",
"description": "Sorting algorithms implemented in JS",
"repository": "neeleshroy/sorting-js",
"author": "Neelesh Roy",
"license": "MIT",
"keywords": [],
"main": "index.js",
"jsnext:main": "index.es6.js",
"babel": {
"presets": [
"es2015",
"stage-0"
],
"plugins": [
"transform-runtime"
]
},
"eslintConfig": {
"parser": "babel-eslint",
"extends": "airbnb/base"
},
"dependencies": {
"babel-runtime": "^6.26.0"
},
"devDependencies": {
"babel-cli": "^6.8.0",
"babel-core": "^6.8.0",
"babel-eslint": "^6.0.4",
"babel-plugin-transform-runtime": "^6.8.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-es2015-rollup": "^3.0.0",
"babel-preset-stage-0": "^6.5.0",
"babel-register": "^6.8.0",
"bs-html-injector": "^3.0.3",
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"del": "^2.2.0",
"eslint": "^2.9.0",
"eslint-config-airbnb": "^8.0.0",
"eslint-plugin-import": "^1.6.1",
"eslint-plugin-jsx-a11y": "^1.0.4",
"eslint-plugin-react": "^5.0.1",
"istanbul": "^1.0.0-alpha.2",
"mocha": "^2.4.5",
"rollup": "^0.26.2",
"rollup-plugin-babel": "^2.4.0",
"sinon": "^2.0.0-pre",
"version-bump-prompt": "^4.1.0"
},
"scripts": {
"lint": "eslint src test tools",
"test": "mocha --compilers js:babel-register",
"test:watch": "mocha --compilers js:babel-register --reporter min --watch",
"test:cover": "babel-node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"build": "node tools/build",
"prepublish": "npm run build",
"publish:docs": "easystatic deploy docs --repo neeleshroy/sorting-js",
"start": "easystatic start docs"
}
"name": "sorting-javascript",
"version": "1.0.5",
"description": "Sorting algorithms implemented in JS",
"repository": "neeleshroy/sorting-js",
"author": "Neelesh Roy",
"license": "MIT",
"keywords": [],
"main": "index.js",
"jsnext:main": "index.es6.js",
"babel": {
"presets": [ "es2015", "stage-0" ],
"plugins": [ "transform-runtime" ]
},
"eslintConfig": {
"parser": "babel-eslint"
},
"dependencies": {
"babel-runtime": "^6.26.0"
},
"devDependencies": {
"babel-cli": "^6.8.0",
"babel-core": "^6.8.0",
"babel-eslint": "^6.0.4",
"babel-plugin-transform-runtime": "^6.8.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-es2015-rollup": "^3.0.0",
"babel-preset-stage-0": "^6.5.0",
"babel-register": "^6.8.0",
"bs-html-injector": "^3.0.3",
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"del": "^2.2.0",
"eslint": "^2.9.0",
"eslint-config-airbnb": "^8.0.0",
"eslint-plugin-import": "^1.6.1",
"eslint-plugin-jsx-a11y": "^1.0.4",
"eslint-plugin-react": "^5.0.1",
"istanbul": "^1.0.0-alpha.2",
"mocha": "^2.4.5",
"rollup": "^0.26.2",
"rollup-plugin-babel": "^2.4.0",
"sinon": "^2.0.0-pre",
"version-bump-prompt": "^4.1.0"
},
"scripts": {
"lint": "eslint src test tools",
"test": "mocha --compilers js:babel-register",
"test:watch": "mocha --compilers js:babel-register --reporter min --watch",
"test:cover": "babel-node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"build": "node tools/build",
"prepublish": "npm run build",
"publish:docs": "easystatic deploy docs --repo neeleshroy/sorting-js",
"start": "easystatic start docs"
}
}
21 changes: 21 additions & 0 deletions src/counting-sort/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const countingSort = (unsorted, min, max) => {
const arr = unsorted;
let i = 0;
let z = 0;
const count = [];

for (i = min; i <= max; i++) {
count[i] = 0;
}

for (i = 0; i < arr.length; i++) {
count[arr[i]]++;
}

for (i = min; i <= max; i++) {
while (count[i]-- > 0) {
arr[z++] = i;
}
}
return arr;
};
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import bubbleSort from './bubble-sort/index';
import insertionSort from './insertion-sort/index';
import selectionSort from './selection-sort/index';
import { mergeSort } from './merge-sort/index';

import { quickSort } from './quick-sort/index';

module.exports = {
ArrayTestBed,
bubbleSort,
insertionSort,
selectionSort,
mergeSort,
quickSort,
};
8 changes: 4 additions & 4 deletions src/quick-sort/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const quickSort = (arr) => {
export const quickSort = (arr) => {
const unsorted = arr;

if (unsorted.length < 1) {
if (unsorted.length <= 1) {
return unsorted;
}

const pivot = unsorted[unsorted - 1];
const pivot = unsorted[unsorted.length - 1];
const left = [];
const right = [];

Expand All @@ -17,5 +17,5 @@ const quickSort = (arr) => {
}
}

return [...quickSort(left), pivot, ...quickSort(right)];
return quickSort(left).concat(pivot, quickSort(right));
};
39 changes: 39 additions & 0 deletions test/counting-sort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* sorting-js
*
* Copyright © 2018 Neelesh Roy. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

import { expect } from 'chai';
import { countingSort } from '../src/counting-sort/index';

describe('Counting Sort', () => {
describe('Counting Sort - function', () => {
it('should return empty array', () => {
const test = [];

const out = countingSort(test, 0, 0);

expect(out).to.eql([]);
});

it('should sort the elements in ascending order', () => {
const test = [ 2, 5, 4, 10, 5, 3, 2, 7 ];

const out = countingSort(test, 2, 10);

expect(out).to.eql([ 2, 2, 3, 4, 5, 5, 7, 10 ]);
});

it('should sort the unique numbers in ascending order', () => {
const test = [ 2, 5, 4, 10, 6, 18, 98, 54 ];

const out = countingSort(test, 2, 98);

expect(out).to.eql([ 2, 4, 5, 6, 10, 18, 54, 98 ]);
});
});
});
49 changes: 49 additions & 0 deletions test/quickSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* sorting-js
*
* Copyright © 2018 Neelesh Roy. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/

import { expect } from 'chai';
import { quickSort } from '../src/quick-sort/index';

describe('Quick Sort', () => {

describe('Quick Sort - function', () => {

it('should return empty array', () => {
const test = [];

const out = quickSort(test);

expect(out).to.eql([]);
});

it('should sort the elements in ascending order', () => {
const test = [2, 5, 4, 10, 5, 3, 2, 7];

const out = quickSort(test);

expect(out).to.eql([2, 2, 3, 4, 5, 5, 7, 10]);
});

it('should sort the string elements', () => {
const test = ['ayda', 'xsy', 'aaa', 'awb', 'cdf'];

const out = quickSort(test);

expect(out).to.eql(['aaa', 'awb', 'ayda', 'cdf', 'xsy']);
});

it('should sort the floats elements', () => {
const test = [1.23, 9.78, 5.34, 3.45, 3.44];

const out = quickSort(test);

expect(out).to.eql([1.23, 3.44, 3.45, 5.34, 9.78]);
});
});
});