From 5320bfc7ec9457c3db42f60e7e068a73e9ff39bb Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Thu, 12 Apr 2018 12:20:39 +0300 Subject: [PATCH] Add BubbleSort. --- src/algorithms/sorting/SortTester.js | 14 ++-- .../sorting/bubble-sort/BubbleSort.js | 13 ++++ .../bubble-sort/__test__/BubbleSort.test.js | 66 +++++++++++++++++++ .../bubble-sort/__test__/bubbleSort.test.js | 45 ------------- 4 files changed, 87 insertions(+), 51 deletions(-) create mode 100644 src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js delete mode 100644 src/algorithms/sorting/bubble-sort/__test__/bubbleSort.test.js diff --git a/src/algorithms/sorting/SortTester.js b/src/algorithms/sorting/SortTester.js index d7d10111..2582940f 100644 --- a/src/algorithms/sorting/SortTester.js +++ b/src/algorithms/sorting/SortTester.js @@ -1,6 +1,7 @@ -export const sortedArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; -export const notSortedArray = [10, 5, 30, -1, 0, 0, 1, 2, -3, 2]; -export const equalArray = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; +export const sortedArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; +export const reverseArr = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; +export const notSortedArr = [15, 8, 5, 12, 10, 1, 16, 9, 11, 7, 20, 3, 2, 6, 17, 18, 4, 13, 14, 19]; +export const equalArr = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; export class SortTester { static testSort(SortingClass) { @@ -11,9 +12,10 @@ export class SortTester { expect(sorter.sort([1, 2])).toEqual([1, 2]); expect(sorter.sort([2, 1])).toEqual([1, 2]); expect(sorter.sort([1, 1, 1])).toEqual([1, 1, 1]); - expect(sorter.sort(sortedArray)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); - expect(sorter.sort(notSortedArray)).toEqual([-3, -1, 0, 0, 1, 2, 2, 5, 10, 30]); - expect(sorter.sort(equalArray)).toEqual([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]); + expect(sorter.sort(sortedArr)).toEqual(sortedArr); + expect(sorter.sort(reverseArr)).toEqual(sortedArr); + expect(sorter.sort(notSortedArr)).toEqual(sortedArr); + expect(sorter.sort(equalArr)).toEqual(equalArr); } static testSortWithCustomComparator(SortingClass) { diff --git a/src/algorithms/sorting/bubble-sort/BubbleSort.js b/src/algorithms/sorting/bubble-sort/BubbleSort.js index 58f7d134..0fda9045 100644 --- a/src/algorithms/sorting/bubble-sort/BubbleSort.js +++ b/src/algorithms/sorting/bubble-sort/BubbleSort.js @@ -2,9 +2,13 @@ import Sort from '../Sort'; 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; for (let i = 0; i < array.length; i += 1) { + swapped = false; + for (let j = 0; j < array.length - 1; j += 1) { // Call visiting callback. this.callbacks.visitingCallback(array[j]); @@ -14,8 +18,17 @@ export default class BubbleSort extends Sort { const tmp = array[j + 1]; array[j + 1] = array[j]; array[j] = tmp; + + // Register the swap. + swapped = true; } } + + // If there were no swaps then array is already sorted and there is + // no need to proceed. + if (!swapped) { + return array; + } } return array; diff --git a/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js b/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js new file mode 100644 index 00000000..df58a743 --- /dev/null +++ b/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js @@ -0,0 +1,66 @@ +import BubbleSort from '../BubbleSort'; +import { + equalArr, + notSortedArr, + reverseArr, + sortedArr, + SortTester, +} from '../../SortTester'; + +describe('BubbleSort', () => { + it('should sort array', () => { + SortTester.testSort(BubbleSort); + }); + + it('should sort array with custom comparator', () => { + SortTester.testSortWithCustomComparator(BubbleSort); + }); + + it('should visit sorted array element specified number of times', () => { + const visitingCallback = jest.fn(); + + const callbacks = { visitingCallback }; + const sorter = new BubbleSort(callbacks); + + const arrayAfterSorting = sorter.sort(sortedArr); + + expect(arrayAfterSorting).toEqual(sortedArr); + expect(visitingCallback).toHaveBeenCalledTimes(19); + }); + + it('should visit not-sorted array element specified number of times', () => { + const visitingCallback = jest.fn(); + + const callbacks = { visitingCallback }; + const sorter = new BubbleSort(callbacks); + + const arrayAfterSorting = sorter.sort(notSortedArr); + + expect(arrayAfterSorting).toEqual(sortedArr); + expect(visitingCallback).toHaveBeenCalledTimes(19); + }); + + it('should visit equal array element specified number of times', () => { + const visitingCallback = jest.fn(); + + const callbacks = { visitingCallback }; + const sorter = new BubbleSort(callbacks); + + const arrayAfterSorting = sorter.sort(equalArr); + + expect(arrayAfterSorting).toEqual(equalArr); + expect(visitingCallback).toHaveBeenCalledTimes(19); + }); + + it('should visit reverse sorted array element specified number of times', () => { + const visitingCallback = jest.fn(); + + const callbacks = { visitingCallback }; + const sorter = new BubbleSort(callbacks); + + const arrayAfterSorting = sorter.sort(reverseArr); + + expect(arrayAfterSorting).toEqual(sortedArr); + expect(visitingCallback).toHaveBeenCalledTimes(19); + }); +}); diff --git a/src/algorithms/sorting/bubble-sort/__test__/bubbleSort.test.js b/src/algorithms/sorting/bubble-sort/__test__/bubbleSort.test.js deleted file mode 100644 index 0c02694e..00000000 --- a/src/algorithms/sorting/bubble-sort/__test__/bubbleSort.test.js +++ /dev/null @@ -1,45 +0,0 @@ -import BubbleSort from '../BubbleSort'; -import { equalArray, notSortedArray, sortedArray, SortTester } from '../../SortTester'; - -describe('bubbleSort', () => { - it('should sort array', () => { - SortTester.testSort(BubbleSort); - }); - - it('should sort array with custom comparator', () => { - SortTester.testSortWithCustomComparator(BubbleSort); - }); - - it('should visit sorted array element specified number of times', () => { - const visitingCallback = jest.fn(); - - const callbacks = { visitingCallback }; - const sorter = new BubbleSort(callbacks); - - sorter.sort(sortedArray); - - expect(visitingCallback).toHaveBeenCalledTimes(90); - }); - - it('should visit not-sorted array element specified number of times', () => { - const visitingCallback = jest.fn(); - - const callbacks = { visitingCallback }; - const sorter = new BubbleSort(callbacks); - - sorter.sort(notSortedArray); - - expect(visitingCallback).toHaveBeenCalledTimes(90); - }); - - it('should visit equal array element specified number of times', () => { - const visitingCallback = jest.fn(); - - const callbacks = { visitingCallback }; - const sorter = new BubbleSort(callbacks); - - sorter.sort(equalArray); - - expect(visitingCallback).toHaveBeenCalledTimes(90); - }); -});