Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 76c172b

Browse files
committed
Code style fixes for negative Counting Sort testing.
1 parent 3be02b5 commit 76c172b

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

‎src/algorithms/sorting/counting-sort/CountingSort.js‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@ import Sort from '../Sort';
33
export default class CountingSort extends Sort {
44
/**
55
* @param {number[]} originalArray
6+
* @param {number} [smallestElement]
67
* @param {number} [biggestElement]
78
*/
8-
sort(originalArray, smallestElement = 0, biggestElement = 0) {
9-
// Detect biggest element in array in order to build number bucket array later.
10-
let detectedSmallestElement = smallestElement;
11-
let detectedBiggestElement = biggestElement;
12-
if (!detectedBiggestElement) {
9+
sort(originalArray, smallestElement = undefined, biggestElement = undefined) {
10+
// Init biggest and smallest elements in array in order to build number bucket array later.
11+
let detectedSmallestElement = smallestElement || 0;
12+
let detectedBiggestElement = biggestElement || 0;
13+
14+
if (smallestElement === undefined || biggestElement === undefined) {
1315
originalArray.forEach((element) => {
1416
// Visit element.
1517
this.callbacks.visitingCallback(element);
1618

19+
// Detect biggest element.
1720
if (this.comparator.greaterThan(element, detectedBiggestElement)) {
1821
detectedBiggestElement = element;
1922
}
20-
if (this.comparator.greaterThan(detectedSmallestElement, element)) {
23+
24+
// Detect smallest element.
25+
if (this.comparator.lessThan(element, detectedSmallestElement)) {
2126
detectedSmallestElement = element;
2227
}
2328
});
@@ -26,6 +31,7 @@ export default class CountingSort extends Sort {
2631
// Init buckets array.
2732
// This array will hold frequency of each number from originalArray.
2833
const buckets = Array(detectedBiggestElement - detectedSmallestElement + 1).fill(0);
34+
2935
originalArray.forEach((element) => {
3036
// Visit element.
3137
this.callbacks.visitingCallback(element);

‎src/algorithms/sorting/counting-sort/__test__/CountingSort.test.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('CountingSort', () => {
2222
SortTester.testNegativeNumbersSort(CountingSort);
2323
});
2424

25-
it('should allow to use specify maximum integer value in array to make sorting faster', () => {
25+
it('should allow to use specify max/min integer value in array to make sorting faster', () => {
2626
const visitingCallback = jest.fn();
2727
const sorter = new CountingSort({ visitingCallback });
2828

@@ -31,7 +31,12 @@ describe('CountingSort', () => {
3131
return element > accumulator ? element : accumulator;
3232
}, 0);
3333

34-
const sortedArray = sorter.sort(notSortedArr, 0, biggestElement);
34+
// Detect smallest number in array in prior.
35+
const smallestElement = notSortedArr.reduce((accumulator, element) => {
36+
return element < accumulator ? element : accumulator;
37+
}, 0);
38+
39+
const sortedArray = sorter.sort(notSortedArr, smallestElement, biggestElement);
3540

3641
expect(sortedArray).toEqual(sortedArr);
3742
// Normally visitingCallback is being called 60 times but in this case

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /