diff --git a/README.md b/README.md index f661801..4cd4041 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 094af14..75c6fe5 100644 --- a/package.json +++ b/package.json @@ -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" + } } diff --git a/src/counting-sort/index.js b/src/counting-sort/index.js new file mode 100644 index 0000000..6bba51c --- /dev/null +++ b/src/counting-sort/index.js @@ -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; +}; diff --git a/src/index.js b/src/index.js index 542d3ee..d102abf 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,7 @@ 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, @@ -11,4 +11,5 @@ module.exports = { insertionSort, selectionSort, mergeSort, + quickSort, }; diff --git a/src/quick-sort/index.js b/src/quick-sort/index.js index 4112be9..dc7c70b 100644 --- a/src/quick-sort/index.js +++ b/src/quick-sort/index.js @@ -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 = []; @@ -17,5 +17,5 @@ const quickSort = (arr) => { } } - return [...quickSort(left), pivot, ...quickSort(right)]; + return quickSort(left).concat(pivot, quickSort(right)); }; diff --git a/test/counting-sort.test.js b/test/counting-sort.test.js new file mode 100644 index 0000000..4cc8199 --- /dev/null +++ b/test/counting-sort.test.js @@ -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 ]); + }); + }); +}); diff --git a/test/quickSort.test.js b/test/quickSort.test.js new file mode 100644 index 0000000..f0974ae --- /dev/null +++ b/test/quickSort.test.js @@ -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]); + }); + }); +});