Skip to content

Commit

Permalink
Code style fixes for negative Counting Sort testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
trekhleb committed Jul 22, 2018
1 parent 3be02b5 commit 76c172b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
18 changes: 12 additions & 6 deletions src/algorithms/sorting/counting-sort/CountingSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@ import Sort from '../Sort';
export default class CountingSort extends Sort {
/**
* @param {number[]} originalArray
* @param {number} [smallestElement]
* @param {number} [biggestElement]
*/
sort(originalArray, smallestElement = 0, biggestElement = 0) {
// Detect biggest element in array in order to build number bucket array later.
let detectedSmallestElement = smallestElement;
let detectedBiggestElement = biggestElement;
if (!detectedBiggestElement) {
sort(originalArray, smallestElement = undefined, biggestElement = undefined) {
// Init biggest and smallest elements in array in order to build number bucket array later.
let detectedSmallestElement = smallestElement || 0;
let detectedBiggestElement = biggestElement || 0;

if (smallestElement === undefined || biggestElement === undefined) {
originalArray.forEach((element) => {
// Visit element.
this.callbacks.visitingCallback(element);

// Detect biggest element.
if (this.comparator.greaterThan(element, detectedBiggestElement)) {
detectedBiggestElement = element;
}
if (this.comparator.greaterThan(detectedSmallestElement, element)) {

// Detect smallest element.
if (this.comparator.lessThan(element, detectedSmallestElement)) {
detectedSmallestElement = element;
}
});
Expand All @@ -26,6 +31,7 @@ export default class CountingSort extends Sort {
// Init buckets array.
// This array will hold frequency of each number from originalArray.
const buckets = Array(detectedBiggestElement - detectedSmallestElement + 1).fill(0);

originalArray.forEach((element) => {
// Visit element.
this.callbacks.visitingCallback(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('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 sorter = new CountingSort({ visitingCallback });

Expand All @@ -31,7 +31,12 @@ describe('CountingSort', () => {
return element > accumulator ? element : accumulator;
}, 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);
// Normally visitingCallback is being called 60 times but in this case
Expand Down

0 comments on commit 76c172b

Please sign in to comment.