From 33963d8e08f3afe437ee0792eb655aea4a79e1c9 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Thu, 12 Apr 2018 14:32:34 +0300 Subject: [PATCH] Add BubbleSort. --- .../breadthFirstSearch.js | 6 +- src/algorithms/sorting/Sort.js | 10 +-- src/algorithms/sorting/SortTester.js | 11 +++- .../sorting/bubble-sort/BubbleSort.js | 3 +- .../bubble-sort/__test__/BubbleSort.test.js | 64 ++++++++----------- 5 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/algorithms/graph/breadth-first-search/breadthFirstSearch.js b/src/algorithms/graph/breadth-first-search/breadthFirstSearch.js index 6ef59c0b..c49ee8a2 100644 --- a/src/algorithms/graph/breadth-first-search/breadthFirstSearch.js +++ b/src/algorithms/graph/breadth-first-search/breadthFirstSearch.js @@ -41,10 +41,10 @@ function initCallbacks(callbacks = {}) { /** * @param {Graph} graph * @param {GraphVertex} startVertex - * @param {Callbacks} [rawCallbacks] + * @param {Callbacks} [originalCallbacks] */ -export default function breadthFirstSearch(graph, startVertex, rawCallbacks) { - const callbacks = initCallbacks(rawCallbacks); +export default function breadthFirstSearch(graph, startVertex, originalCallbacks) { + const callbacks = initCallbacks(originalCallbacks); const vertexQueue = new Queue(); // Do initial queue setup. diff --git a/src/algorithms/sorting/Sort.js b/src/algorithms/sorting/Sort.js index 4d969acc..e0290881 100644 --- a/src/algorithms/sorting/Sort.js +++ b/src/algorithms/sorting/Sort.js @@ -9,17 +9,17 @@ import Comparator from '../../utils/comparator/Comparator'; */ export default class Sort { - constructor(rawCallbacks) { - this.callbacks = Sort.initSortingCallbacks(rawCallbacks); + constructor(originalCallbacks) { + this.callbacks = Sort.initSortingCallbacks(originalCallbacks); this.comparator = new Comparator(this.callbacks.compareCallback); } /** - * @param {SorterCallbacks} rawCallbacks + * @param {SorterCallbacks} originalCallbacks * @returns {SorterCallbacks} */ - static initSortingCallbacks(rawCallbacks) { - const callbacks = rawCallbacks || {}; + static initSortingCallbacks(originalCallbacks) { + const callbacks = originalCallbacks || {}; const stubCallback = () => {}; callbacks.compareCallback = callbacks.compareCallback || undefined; diff --git a/src/algorithms/sorting/SortTester.js b/src/algorithms/sorting/SortTester.js index 2582940f..b8dc1eee 100644 --- a/src/algorithms/sorting/SortTester.js +++ b/src/algorithms/sorting/SortTester.js @@ -24,7 +24,6 @@ export class SortTester { if (a.length === b.length) { return 0; } - return a.length < b.length ? -1 : 1; }, }; @@ -36,4 +35,14 @@ export class SortTester { expect(sorter.sort(['aa', 'a'])).toEqual(['a', 'aa']); expect(sorter.sort(['bb', 'aa', 'c'])).toEqual(['c', 'bb', 'aa']); } + + static testAlgorithmTimeComplexity(SortingClass, arrayToBeSorted, numberOfVisits) { + const visitingCallback = jest.fn(); + const callbacks = { visitingCallback }; + const sorter = new SortingClass(callbacks); + + sorter.sort(arrayToBeSorted); + + expect(visitingCallback).toHaveBeenCalledTimes(numberOfVisits); + } } diff --git a/src/algorithms/sorting/bubble-sort/BubbleSort.js b/src/algorithms/sorting/bubble-sort/BubbleSort.js index 0fda9045..dd7c026b 100644 --- a/src/algorithms/sorting/bubble-sort/BubbleSort.js +++ b/src/algorithms/sorting/bubble-sort/BubbleSort.js @@ -4,7 +4,8 @@ export default class BubbleSort extends Sort { sort(initialArray) { // Flag that holds info about whether the swap has occur or not. let swapped = false; - const array = initialArray; + // Clone original array to prevent its modification. + const array = initialArray.slice(0); for (let i = 0; i < array.length; i += 1) { swapped = false; diff --git a/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js b/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js index df58a743..d2049f4f 100644 --- a/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js +++ b/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js @@ -16,51 +16,43 @@ describe('BubbleSort', () => { SortTester.testSortWithCustomComparator(BubbleSort); }); - it('should visit sorted array element specified number of times', () => { - const visitingCallback = jest.fn(); + it('should visit EQUAL array element specified number of times', () => { + const expectedNumberOfVisits = 19; - const callbacks = { visitingCallback }; - const sorter = new BubbleSort(callbacks); - - const arrayAfterSorting = sorter.sort(sortedArr); - - expect(arrayAfterSorting).toEqual(sortedArr); - expect(visitingCallback).toHaveBeenCalledTimes(19); + SortTester.testAlgorithmTimeComplexity( + BubbleSort, + equalArr, + expectedNumberOfVisits, + ); }); - it('should visit not-sorted array element specified number of times', () => { - const visitingCallback = jest.fn(); + it('should visit SORTED array element specified number of times', () => { + const expectedNumberOfVisits = 19; - const callbacks = { visitingCallback }; - const sorter = new BubbleSort(callbacks); - - const arrayAfterSorting = sorter.sort(notSortedArr); - - expect(arrayAfterSorting).toEqual(sortedArr); - expect(visitingCallback).toHaveBeenCalledTimes(19); + SortTester.testAlgorithmTimeComplexity( + BubbleSort, + sortedArr, + expectedNumberOfVisits, + ); }); - it('should visit equal array element specified number of times', () => { - const visitingCallback = jest.fn(); + it('should visit NOT SORTED array element specified number of times', () => { + const expectedNumberOfVisits = 266; - const callbacks = { visitingCallback }; - const sorter = new BubbleSort(callbacks); - - const arrayAfterSorting = sorter.sort(equalArr); - - expect(arrayAfterSorting).toEqual(equalArr); - expect(visitingCallback).toHaveBeenCalledTimes(19); + SortTester.testAlgorithmTimeComplexity( + BubbleSort, + notSortedArr, + expectedNumberOfVisits, + ); }); - it('should visit reverse sorted array element specified number of times', () => { - const visitingCallback = jest.fn(); + it('should visit REVERSE SORTED array element specified number of times', () => { + const expectedNumberOfVisits = 380; - const callbacks = { visitingCallback }; - const sorter = new BubbleSort(callbacks); - - const arrayAfterSorting = sorter.sort(reverseArr); - - expect(arrayAfterSorting).toEqual(sortedArr); - expect(visitingCallback).toHaveBeenCalledTimes(19); + SortTester.testAlgorithmTimeComplexity( + BubbleSort, + reverseArr, + expectedNumberOfVisits, + ); }); });