mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-27 15:41:16 +08:00
Code style fixes for negative Counting Sort testing.
This commit is contained in:
parent
3be02b5519
commit
76c172b20b
@ -3,21 +3,26 @@ import Sort from '../Sort';
|
|||||||
export default class CountingSort extends Sort {
|
export default class CountingSort extends Sort {
|
||||||
/**
|
/**
|
||||||
* @param {number[]} originalArray
|
* @param {number[]} originalArray
|
||||||
|
* @param {number} [smallestElement]
|
||||||
* @param {number} [biggestElement]
|
* @param {number} [biggestElement]
|
||||||
*/
|
*/
|
||||||
sort(originalArray, smallestElement = 0, biggestElement = 0) {
|
sort(originalArray, smallestElement = undefined, biggestElement = undefined) {
|
||||||
// Detect biggest element in array in order to build number bucket array later.
|
// Init biggest and smallest elements in array in order to build number bucket array later.
|
||||||
let detectedSmallestElement = smallestElement;
|
let detectedSmallestElement = smallestElement || 0;
|
||||||
let detectedBiggestElement = biggestElement;
|
let detectedBiggestElement = biggestElement || 0;
|
||||||
if (!detectedBiggestElement) {
|
|
||||||
|
if (smallestElement === undefined || biggestElement === undefined) {
|
||||||
originalArray.forEach((element) => {
|
originalArray.forEach((element) => {
|
||||||
// Visit element.
|
// Visit element.
|
||||||
this.callbacks.visitingCallback(element);
|
this.callbacks.visitingCallback(element);
|
||||||
|
|
||||||
|
// Detect biggest element.
|
||||||
if (this.comparator.greaterThan(element, detectedBiggestElement)) {
|
if (this.comparator.greaterThan(element, detectedBiggestElement)) {
|
||||||
detectedBiggestElement = element;
|
detectedBiggestElement = element;
|
||||||
}
|
}
|
||||||
if (this.comparator.greaterThan(detectedSmallestElement, element)) {
|
|
||||||
|
// Detect smallest element.
|
||||||
|
if (this.comparator.lessThan(element, detectedSmallestElement)) {
|
||||||
detectedSmallestElement = element;
|
detectedSmallestElement = element;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -26,6 +31,7 @@ export default class CountingSort extends Sort {
|
|||||||
// Init buckets array.
|
// Init buckets array.
|
||||||
// This array will hold frequency of each number from originalArray.
|
// This array will hold frequency of each number from originalArray.
|
||||||
const buckets = Array(detectedBiggestElement - detectedSmallestElement + 1).fill(0);
|
const buckets = Array(detectedBiggestElement - detectedSmallestElement + 1).fill(0);
|
||||||
|
|
||||||
originalArray.forEach((element) => {
|
originalArray.forEach((element) => {
|
||||||
// Visit element.
|
// Visit element.
|
||||||
this.callbacks.visitingCallback(element);
|
this.callbacks.visitingCallback(element);
|
||||||
|
@ -22,7 +22,7 @@ describe('CountingSort', () => {
|
|||||||
SortTester.testNegativeNumbersSort(CountingSort);
|
SortTester.testNegativeNumbersSort(CountingSort);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should allow to use specify maximum integer value in array to make sorting faster', () => {
|
it('should allow to use specify max/min integer value in array to make sorting faster', () => {
|
||||||
const visitingCallback = jest.fn();
|
const visitingCallback = jest.fn();
|
||||||
const sorter = new CountingSort({ visitingCallback });
|
const sorter = new CountingSort({ visitingCallback });
|
||||||
|
|
||||||
@ -31,7 +31,12 @@ describe('CountingSort', () => {
|
|||||||
return element > accumulator ? element : accumulator;
|
return element > accumulator ? element : accumulator;
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
||||||
const sortedArray = sorter.sort(notSortedArr, 0, biggestElement);
|
// Detect smallest number in array in prior.
|
||||||
|
const smallestElement = notSortedArr.reduce((accumulator, element) => {
|
||||||
|
return element < accumulator ? element : accumulator;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
const sortedArray = sorter.sort(notSortedArr, smallestElement, biggestElement);
|
||||||
|
|
||||||
expect(sortedArray).toEqual(sortedArr);
|
expect(sortedArray).toEqual(sortedArr);
|
||||||
// Normally visitingCallback is being called 60 times but in this case
|
// Normally visitingCallback is being called 60 times but in this case
|
||||||
|
Loading…
Reference in New Issue
Block a user