From 1f7dfb875cb91c8dc733193cf5019da2083f238b Mon Sep 17 00:00:00 2001 From: edumoreira1506 Date: Thu, 12 Dec 2019 14:44:29 -0300 Subject: [PATCH] Add BogoSort --- src/algorithms/sorting/bogo-sort/BogoSort.js | 41 +++++++++++++ src/algorithms/sorting/bogo-sort/README.md | 0 .../bogo-sort/__test__/BogoSort.test.js | 59 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 src/algorithms/sorting/bogo-sort/BogoSort.js create mode 100644 src/algorithms/sorting/bogo-sort/README.md create mode 100644 src/algorithms/sorting/bogo-sort/__test__/BogoSort.test.js diff --git a/src/algorithms/sorting/bogo-sort/BogoSort.js b/src/algorithms/sorting/bogo-sort/BogoSort.js new file mode 100644 index 00000000..b9c29b49 --- /dev/null +++ b/src/algorithms/sorting/bogo-sort/BogoSort.js @@ -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; + } +} diff --git a/src/algorithms/sorting/bogo-sort/README.md b/src/algorithms/sorting/bogo-sort/README.md new file mode 100644 index 00000000..e69de29b diff --git a/src/algorithms/sorting/bogo-sort/__test__/BogoSort.test.js b/src/algorithms/sorting/bogo-sort/__test__/BogoSort.test.js new file mode 100644 index 00000000..87ef8c35 --- /dev/null +++ b/src/algorithms/sorting/bogo-sort/__test__/BogoSort.test.js @@ -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, + ); + }); +});