allalgorithms
Advanced tools
| const Comparator = require('../util/comparator'); | ||
| /* HELPER-FUNCTION | ||
| IsSort : checks whether the given array is sorted (ascending) | ||
| Returns true if the given array is ascending sorted otherwise false | ||
| */ | ||
| const isSorted = (a, comparator) => { | ||
| for (let i = 0; i < a.size - 1; i++) { | ||
| if (comparator.greaterThan(a[i], a[i + 1])) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| }; | ||
| /* | ||
| HELPER-FUNCTION | ||
| RandInt : returns a random integer between 0 and N-1 | ||
| */ | ||
| const randInt = N => { | ||
| return Math.floor(Math.random() * N); | ||
| }; | ||
| /* | ||
| BogoSort: bogo-sort (stupid-sort) algorithm for a given array 'a' | ||
| */ | ||
| const bogoSort = (arr, compareFunction) => { | ||
| const comparator = new Comparator(compareFunction); | ||
| while (!isSorted(arr, comparator)) { | ||
| const a = randInt(arr.size); | ||
| const b = randInt(arr.size); | ||
| const tmp = arr[a]; | ||
| arr[a] = arr[b]; | ||
| arr[b] = tmp; | ||
| } | ||
| return arr; | ||
| }; | ||
| module.exports = bogoSort; | ||
+14
-17
@@ -1,20 +0,17 @@ | ||
| <div align="left"> | ||
| <h1><img src="https://cdn.abranhe.com/projects/algorithms/algorithms.svg" alt="Algorithms" height="30px"> | ||
| <img src="https://cdn.abranhe.com/projects/algorithms/logos/javascript.svg" width="30px"> | ||
| Changelog | ||
| </h1> | ||
| <div> | ||
| n.n.n / 2019-02-21 | ||
| ================== | ||
| # `0.0.2` | ||
| * Merge pull request #2 from christianbender/add_bogo_sort | ||
| - Fix installing issue | ||
| * changed according to xo-tool | ||
| * added bogo sort | ||
| * package.json | ||
| * Merge pull request #1 from abranhe/donotdisturb | ||
| * no more notifications | ||
| * remove comma (,) | ||
| # `0.0.1` | ||
| Date: October 29, 2018 | ||
| > Algorithms: | ||
| - **Sorting** | ||
| - Bubble Sort | ||
| - Merge Sort | ||
| 0.0.2 / 2018-10-29 | ||
| ================== | ||
| * fix install issue && jump to 0.0.2 | ||
| * init |
+1
-1
| module.exports = { | ||
| sorting: require('./src/sorting'), | ||
| sorting: require('./src/sorting') | ||
| }; |
+3
-3
| { | ||
| "name": "allalgorithms", | ||
| "version": "0.0.2", | ||
| "version": "0.0.3", | ||
| "description": "A collection of All ▲lgorithms implemented in JavaScript", | ||
@@ -14,4 +14,4 @@ "main": "index.js", | ||
| "files": [ | ||
| "index.js", | ||
| "src/" | ||
| "index.js", | ||
| "src/" | ||
| ], | ||
@@ -18,0 +18,0 @@ "keywords": [ |
+1
-0
| module.exports = { | ||
| bogoSort: require('./sorting/bogo-sort'), | ||
| bubbleSort: require('./sorting/bubble-sort'), | ||
| mergeSort: require('./sorting/merge-sort') | ||
| }; |
@@ -15,1 +15,6 @@ import test from 'ava'; | ||
| }); | ||
| test('Testing Bogo Sort', t => { | ||
| t.deepEqual(expected, sorting.bogoSort(array)); | ||
| }); | ||
10585
10.74%12
9.09%204
23.64%