mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Add BubbleSort.
This commit is contained in:
parent
5320bfc7ec
commit
33963d8e08
@ -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.
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user