Skip to content

Added bogo-sort plus tests #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 22, 2019
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"url": "git+https://github.com/abranhe/allalgorithms-js.git"
},
"files": [
"index.js",
"src/"
"index.js",
"src/"
],
"keywords": [
"computer-science",
Expand Down
3 changes: 2 additions & 1 deletion src/sorting.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
bubbleSort: require('./sorting/bubble-sort'),
mergeSort: require('./sorting/merge-sort')
mergeSort: require('./sorting/merge-sort'),
bogoSort: require('./sorting/bogo-sort')
};
42 changes: 42 additions & 0 deletions src/sorting/bogo-sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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;

5 changes: 5 additions & 0 deletions src/test/sorting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ test('Testing Bubble Sort', t => {
test('Testing Merge Sort', t => {
t.deepEqual(expected, sorting.mergeSort(array));
});

test('Testing Bogo Sort', t => {
t.deepEqual(expected, sorting.bogoSort(array));
});

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy