mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Compare commits
2 Commits
300935133c
...
7c48d29a19
Author | SHA1 | Date | |
---|---|---|---|
|
7c48d29a19 | ||
|
1f7dfb875c |
41
src/algorithms/sorting/bogo-sort/BogoSort.js
Normal file
41
src/algorithms/sorting/bogo-sort/BogoSort.js
Normal file
@ -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;
|
||||
}
|
||||
}
|
0
src/algorithms/sorting/bogo-sort/README.md
Normal file
0
src/algorithms/sorting/bogo-sort/README.md
Normal file
59
src/algorithms/sorting/bogo-sort/__test__/BogoSort.test.js
Normal file
59
src/algorithms/sorting/bogo-sort/__test__/BogoSort.test.js
Normal file
@ -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,
|
||||
);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user