Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: edumoreira1506/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: feature/bogo-sort
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 1 commit
  • 3 files changed
  • 1 contributor

Commits on Dec 12, 2019

  1. Add BogoSort

    edumoreira1506 committed Dec 12, 2019
    Copy the full SHA
    1f7dfb8 View commit details
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,
);
});
});