diff --git a/src/algorithms/sorting/SortTester.js b/src/algorithms/sorting/SortTester.js index 4a6c3492..e92ea39a 100644 --- a/src/algorithms/sorting/SortTester.js +++ b/src/algorithms/sorting/SortTester.js @@ -2,6 +2,8 @@ export const sortedArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, export const reverseArr = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; export const notSortedArr = [15, 8, 5, 12, 10, 1, 16, 9, 11, 7, 20, 3, 2, 6, 17, 18, 4, 13, 14, 19]; export const equalArr = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; +export const negativeArr = [-1, 0, 5, -10, 20, 13, -7, 3, 2, -3]; +export const negativeArrSorted = [-10, -7, -3, -1, 0, 2, 3, 5, 13, 20]; export class SortTester { static testSort(SortingClass) { @@ -18,6 +20,11 @@ export class SortTester { expect(sorter.sort(equalArr)).toEqual(equalArr); } + static testNegativeNumbersSort(SortingClass) { + const sorter = new SortingClass(); + expect(sorter.sort(negativeArr)).toEqual(negativeArrSorted); + } + static testSortWithCustomComparator(SortingClass) { const callbacks = { compareCallback: (a, b) => { diff --git a/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js b/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js index 5ffd6529..7e71a0b6 100644 --- a/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js +++ b/src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js @@ -26,6 +26,10 @@ describe('BubbleSort', () => { SortTester.testSortStability(BubbleSort); }); + it('should sort negative numbers', () => { + SortTester.testNegativeNumbersSort(BubbleSort); + }); + it('should visit EQUAL array element specified number of times', () => { SortTester.testAlgorithmTimeComplexity( BubbleSort, diff --git a/src/algorithms/sorting/counting-sort/CountingSort.js b/src/algorithms/sorting/counting-sort/CountingSort.js index f08f4654..2ef3171e 100644 --- a/src/algorithms/sorting/counting-sort/CountingSort.js +++ b/src/algorithms/sorting/counting-sort/CountingSort.js @@ -6,8 +6,7 @@ export default class CountingSort extends Sort { * @param {number} [biggestElement] */ sort(originalArray, biggestElement = 0) { - // Detect biggest element in array in order to build in order to build - // number bucket array later. + // Detect biggest element in array in order to build number bucket array later. let detectedBiggestElement = biggestElement; if (!detectedBiggestElement) { originalArray.forEach((element) => { diff --git a/src/algorithms/sorting/heap-sort/__test__/HeapSort.test.js b/src/algorithms/sorting/heap-sort/__test__/HeapSort.test.js index 1b877aee..75f690d4 100644 --- a/src/algorithms/sorting/heap-sort/__test__/HeapSort.test.js +++ b/src/algorithms/sorting/heap-sort/__test__/HeapSort.test.js @@ -24,6 +24,10 @@ describe('HeapSort', () => { SortTester.testSortWithCustomComparator(HeapSort); }); + it('should sort negative numbers', () => { + SortTester.testNegativeNumbersSort(HeapSort); + }); + it('should visit EQUAL array element specified number of times', () => { SortTester.testAlgorithmTimeComplexity( HeapSort, diff --git a/src/algorithms/sorting/insertion-sort/__test__/InsertionSort.test.js b/src/algorithms/sorting/insertion-sort/__test__/InsertionSort.test.js index 4fb96e2f..588472e2 100644 --- a/src/algorithms/sorting/insertion-sort/__test__/InsertionSort.test.js +++ b/src/algorithms/sorting/insertion-sort/__test__/InsertionSort.test.js @@ -26,6 +26,10 @@ describe('InsertionSort', () => { SortTester.testSortStability(InsertionSort); }); + it('should sort negative numbers', () => { + SortTester.testNegativeNumbersSort(InsertionSort); + }); + it('should visit EQUAL array element specified number of times', () => { SortTester.testAlgorithmTimeComplexity( InsertionSort, diff --git a/src/algorithms/sorting/merge-sort/__test__/MergeSort.test.js b/src/algorithms/sorting/merge-sort/__test__/MergeSort.test.js index 1f508c47..0112003b 100644 --- a/src/algorithms/sorting/merge-sort/__test__/MergeSort.test.js +++ b/src/algorithms/sorting/merge-sort/__test__/MergeSort.test.js @@ -26,6 +26,10 @@ describe('MergeSort', () => { SortTester.testSortStability(MergeSort); }); + it('should sort negative numbers', () => { + SortTester.testNegativeNumbersSort(MergeSort); + }); + it('should visit EQUAL array element specified number of times', () => { SortTester.testAlgorithmTimeComplexity( MergeSort, diff --git a/src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js b/src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js index 0acca3e1..71c1fe71 100644 --- a/src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js +++ b/src/algorithms/sorting/quick-sort/__test__/QuickSort.test.js @@ -26,6 +26,10 @@ describe('QuickSort', () => { SortTester.testSortStability(QuickSort); }); + it('should sort negative numbers', () => { + SortTester.testNegativeNumbersSort(QuickSort); + }); + it('should visit EQUAL array element specified number of times', () => { SortTester.testAlgorithmTimeComplexity( QuickSort, diff --git a/src/algorithms/sorting/quick-sort/__test__/QuickSortInPlace.test.js b/src/algorithms/sorting/quick-sort/__test__/QuickSortInPlace.test.js index 6e78ef29..0e103f2e 100644 --- a/src/algorithms/sorting/quick-sort/__test__/QuickSortInPlace.test.js +++ b/src/algorithms/sorting/quick-sort/__test__/QuickSortInPlace.test.js @@ -22,6 +22,10 @@ describe('QuickSortInPlace', () => { SortTester.testSortWithCustomComparator(QuickSortInPlace); }); + it('should sort negative numbers', () => { + SortTester.testNegativeNumbersSort(QuickSortInPlace); + }); + it('should visit EQUAL array element specified number of times', () => { SortTester.testAlgorithmTimeComplexity( QuickSortInPlace, diff --git a/src/algorithms/sorting/selection-sort/__test__/SelectionSort.test.js b/src/algorithms/sorting/selection-sort/__test__/SelectionSort.test.js index 4a56aa9c..d52385d1 100644 --- a/src/algorithms/sorting/selection-sort/__test__/SelectionSort.test.js +++ b/src/algorithms/sorting/selection-sort/__test__/SelectionSort.test.js @@ -22,6 +22,10 @@ describe('SelectionSort', () => { SortTester.testSortWithCustomComparator(SelectionSort); }); + it('should sort negative numbers', () => { + SortTester.testNegativeNumbersSort(SelectionSort); + }); + it('should visit EQUAL array element specified number of times', () => { SortTester.testAlgorithmTimeComplexity( SelectionSort, diff --git a/src/algorithms/sorting/shell-sort/__test__/ShellSort.test.js b/src/algorithms/sorting/shell-sort/__test__/ShellSort.test.js index c05aa6cd..454845f4 100644 --- a/src/algorithms/sorting/shell-sort/__test__/ShellSort.test.js +++ b/src/algorithms/sorting/shell-sort/__test__/ShellSort.test.js @@ -22,6 +22,10 @@ describe('ShellSort', () => { SortTester.testSortWithCustomComparator(ShellSort); }); + it('should sort negative numbers', () => { + SortTester.testNegativeNumbersSort(ShellSort); + }); + it('should visit EQUAL array element specified number of times', () => { SortTester.testAlgorithmTimeComplexity( ShellSort,