Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 33963d8

Browse files
committedApr 12, 2018
Add BubbleSort.
1 parent 5320bfc commit 33963d8

File tree

5 files changed

+49
-47
lines changed

5 files changed

+49
-47
lines changed
 

‎src/algorithms/graph/breadth-first-search/breadthFirstSearch.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ function initCallbacks(callbacks = {}) {
4141
/**
4242
* @param {Graph} graph
4343
* @param {GraphVertex} startVertex
44-
* @param {Callbacks} [rawCallbacks]
44+
* @param {Callbacks} [originalCallbacks]
4545
*/
46-
export default function breadthFirstSearch(graph, startVertex, rawCallbacks) {
47-
const callbacks = initCallbacks(rawCallbacks);
46+
export default function breadthFirstSearch(graph, startVertex, originalCallbacks) {
47+
const callbacks = initCallbacks(originalCallbacks);
4848
const vertexQueue = new Queue();
4949

5050
// Do initial queue setup.

‎src/algorithms/sorting/Sort.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import Comparator from '../../utils/comparator/Comparator';
99
*/
1010

1111
export default class Sort {
12-
constructor(rawCallbacks) {
13-
this.callbacks = Sort.initSortingCallbacks(rawCallbacks);
12+
constructor(originalCallbacks) {
13+
this.callbacks = Sort.initSortingCallbacks(originalCallbacks);
1414
this.comparator = new Comparator(this.callbacks.compareCallback);
1515
}
1616

1717
/**
18-
* @param {SorterCallbacks} rawCallbacks
18+
* @param {SorterCallbacks} originalCallbacks
1919
* @returns {SorterCallbacks}
2020
*/
21-
static initSortingCallbacks(rawCallbacks) {
22-
const callbacks = rawCallbacks || {};
21+
static initSortingCallbacks(originalCallbacks) {
22+
const callbacks = originalCallbacks || {};
2323
const stubCallback = () => {};
2424

2525
callbacks.compareCallback = callbacks.compareCallback || undefined;

‎src/algorithms/sorting/SortTester.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export class SortTester {
2424
if (a.length === b.length) {
2525
return 0;
2626
}
27-
2827
return a.length < b.length ? -1 : 1;
2928
},
3029
};
@@ -36,4 +35,14 @@ export class SortTester {
3635
expect(sorter.sort(['aa', 'a'])).toEqual(['a', 'aa']);
3736
expect(sorter.sort(['bb', 'aa', 'c'])).toEqual(['c', 'bb', 'aa']);
3837
}
38+
39+
static testAlgorithmTimeComplexity(SortingClass, arrayToBeSorted, numberOfVisits) {
40+
const visitingCallback = jest.fn();
41+
const callbacks = { visitingCallback };
42+
const sorter = new SortingClass(callbacks);
43+
44+
sorter.sort(arrayToBeSorted);
45+
46+
expect(visitingCallback).toHaveBeenCalledTimes(numberOfVisits);
47+
}
3948
}

‎src/algorithms/sorting/bubble-sort/BubbleSort.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export default class BubbleSort extends Sort {
44
sort(initialArray) {
55
// Flag that holds info about whether the swap has occur or not.
66
let swapped = false;
7-
const array = initialArray;
7+
// Clone original array to prevent its modification.
8+
const array = initialArray.slice(0);
89

910
for (let i = 0; i < array.length; i += 1) {
1011
swapped = false;

‎src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js

+29-37
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,43 @@ describe('BubbleSort', () => {
1616
SortTester.testSortWithCustomComparator(BubbleSort);
1717
});
1818

19-
it('should visit sorted array element specified number of times', () => {
20-
const visitingCallback = jest.fn();
21-
22-
const callbacks = { visitingCallback };
23-
const sorter = new BubbleSort(callbacks);
24-
25-
const arrayAfterSorting = sorter.sort(sortedArr);
26-
27-
expect(arrayAfterSorting).toEqual(sortedArr);
28-
expect(visitingCallback).toHaveBeenCalledTimes(19);
19+
it('should visit EQUAL array element specified number of times', () => {
20+
const expectedNumberOfVisits = 19;
21+
22+
SortTester.testAlgorithmTimeComplexity(
23+
BubbleSort,
24+
equalArr,
25+
expectedNumberOfVisits,
26+
);
2927
});
3028

31-
it('should visit not-sorted array element specified number of times', () => {
32-
const visitingCallback = jest.fn();
33-
34-
const callbacks = { visitingCallback };
35-
const sorter = new BubbleSort(callbacks);
36-
37-
const arrayAfterSorting = sorter.sort(notSortedArr);
29+
it('should visit SORTED array element specified number of times', () => {
30+
const expectedNumberOfVisits = 19;
3831

39-
expect(arrayAfterSorting).toEqual(sortedArr);
40-
expect(visitingCallback).toHaveBeenCalledTimes(19);
32+
SortTester.testAlgorithmTimeComplexity(
33+
BubbleSort,
34+
sortedArr,
35+
expectedNumberOfVisits,
36+
);
4137
});
4238

43-
it('should visit equal array element specified number of times', () => {
44-
const visitingCallback = jest.fn();
39+
it('should visit NOT SORTED array element specified number of times', () => {
40+
const expectedNumberOfVisits = 266;
4541

46-
const callbacks = { visitingCallback };
47-
const sorter = new BubbleSort(callbacks);
48-
49-
const arrayAfterSorting = sorter.sort(equalArr);
50-
51-
expect(arrayAfterSorting).toEqual(equalArr);
52-
expect(visitingCallback).toHaveBeenCalledTimes(19);
42+
SortTester.testAlgorithmTimeComplexity(
43+
BubbleSort,
44+
notSortedArr,
45+
expectedNumberOfVisits,
46+
);
5347
});
5448

55-
it('should visit reverse sorted array element specified number of times', () => {
56-
const visitingCallback = jest.fn();
57-
58-
const callbacks = { visitingCallback };
59-
const sorter = new BubbleSort(callbacks);
60-
61-
const arrayAfterSorting = sorter.sort(reverseArr);
49+
it('should visit REVERSE SORTED array element specified number of times', () => {
50+
const expectedNumberOfVisits = 380;
6251

63-
expect(arrayAfterSorting).toEqual(sortedArr);
64-
expect(visitingCallback).toHaveBeenCalledTimes(19);
52+
SortTester.testAlgorithmTimeComplexity(
53+
BubbleSort,
54+
reverseArr,
55+
expectedNumberOfVisits,
56+
);
6557
});
6658
});

0 commit comments

Comments
 (0)
Please sign in to comment.