Skip to content
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

Add BogoSort #435

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions src/algorithms/sorting/bogo-sort/BogoSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Sort from '../Sort';

export default class BogoSort extends Sort {
sort(originalArray) {
// Clone original array to prevent its modification.
let array = [...originalArray];

while (!this.isSorted(array)) { // Verification if is sorted
array = [...this.shuffle(array)]; // Execute shuffle
}

return array;
}

isSorted(array) {
for (let i = 0; i < array.length; i += 1) {
if (array[i - 1] > array[i]) {
return false;
}
}
return true;
}

shuffle(array) {
const newArray = [...array];
let count = array.length;
let index;
let aux;

while (count > 0) {
index = Math.floor(Math.random() * count);
count -= 1;

aux = newArray[count];
newArray[count] = newArray[index];
newArray[index] = aux;
}

return newArray;
}
}
Empty file.
59 changes: 59 additions & 0 deletions src/algorithms/sorting/bogo-sort/__test__/BogoSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import BogoSort from '../BogoSort';
import {
equalArr,
notSortedArr,
reverseArr,
sortedArr,
SortTester,
} from '../../SortTester';

const SORTED_ARRAY_VISITING_COUNT = 40;
const NOT_SORTED_ARRAY_VISITING_COUNT = 40;
const REVERSE_SORTED_ARRAY_VISITING_COUNT = 40;
const EQUAL_ARRAY_VISITING_COUNT = 40;

describe('BogoSort', () => {
it('should sort array', () => {
SortTester.testSort(BogoSort);
});

it('should sort array with custom comparator', () => {
SortTester.testSortWithCustomComparator(BogoSort);
});

it('should sort negative numbers', () => {
SortTester.testNegativeNumbersSort(BogoSort);
});

it('should visit EQUAL array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
BogoSort,
equalArr,
EQUAL_ARRAY_VISITING_COUNT,
);
});

it('should visit SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
BogoSort,
sortedArr,
SORTED_ARRAY_VISITING_COUNT,
);
});

it('should visit NOT SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
BogoSort,
notSortedArr,
NOT_SORTED_ARRAY_VISITING_COUNT,
);
});

it('should visit REVERSE SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
BogoSort,
reverseArr,
REVERSE_SORTED_ARRAY_VISITING_COUNT,
);
});
});