mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-27 15:41:16 +08:00
Add test cases for sorting negative numbers and zeros.
This commit is contained in:
parent
d82958dea9
commit
93bfe97e27
@ -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 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 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 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 {
|
export class SortTester {
|
||||||
static testSort(SortingClass) {
|
static testSort(SortingClass) {
|
||||||
@ -18,6 +20,11 @@ export class SortTester {
|
|||||||
expect(sorter.sort(equalArr)).toEqual(equalArr);
|
expect(sorter.sort(equalArr)).toEqual(equalArr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static testNegativeNumbersSort(SortingClass) {
|
||||||
|
const sorter = new SortingClass();
|
||||||
|
expect(sorter.sort(negativeArr)).toEqual(negativeArrSorted);
|
||||||
|
}
|
||||||
|
|
||||||
static testSortWithCustomComparator(SortingClass) {
|
static testSortWithCustomComparator(SortingClass) {
|
||||||
const callbacks = {
|
const callbacks = {
|
||||||
compareCallback: (a, b) => {
|
compareCallback: (a, b) => {
|
||||||
|
@ -26,6 +26,10 @@ describe('BubbleSort', () => {
|
|||||||
SortTester.testSortStability(BubbleSort);
|
SortTester.testSortStability(BubbleSort);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should sort negative numbers', () => {
|
||||||
|
SortTester.testNegativeNumbersSort(BubbleSort);
|
||||||
|
});
|
||||||
|
|
||||||
it('should visit EQUAL array element specified number of times', () => {
|
it('should visit EQUAL array element specified number of times', () => {
|
||||||
SortTester.testAlgorithmTimeComplexity(
|
SortTester.testAlgorithmTimeComplexity(
|
||||||
BubbleSort,
|
BubbleSort,
|
||||||
|
@ -6,8 +6,7 @@ export default class CountingSort extends Sort {
|
|||||||
* @param {number} [biggestElement]
|
* @param {number} [biggestElement]
|
||||||
*/
|
*/
|
||||||
sort(originalArray, biggestElement = 0) {
|
sort(originalArray, biggestElement = 0) {
|
||||||
// Detect biggest element in array in order to build in order to build
|
// Detect biggest element in array in order to build number bucket array later.
|
||||||
// number bucket array later.
|
|
||||||
let detectedBiggestElement = biggestElement;
|
let detectedBiggestElement = biggestElement;
|
||||||
if (!detectedBiggestElement) {
|
if (!detectedBiggestElement) {
|
||||||
originalArray.forEach((element) => {
|
originalArray.forEach((element) => {
|
||||||
|
@ -24,6 +24,10 @@ describe('HeapSort', () => {
|
|||||||
SortTester.testSortWithCustomComparator(HeapSort);
|
SortTester.testSortWithCustomComparator(HeapSort);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should sort negative numbers', () => {
|
||||||
|
SortTester.testNegativeNumbersSort(HeapSort);
|
||||||
|
});
|
||||||
|
|
||||||
it('should visit EQUAL array element specified number of times', () => {
|
it('should visit EQUAL array element specified number of times', () => {
|
||||||
SortTester.testAlgorithmTimeComplexity(
|
SortTester.testAlgorithmTimeComplexity(
|
||||||
HeapSort,
|
HeapSort,
|
||||||
|
@ -26,6 +26,10 @@ describe('InsertionSort', () => {
|
|||||||
SortTester.testSortStability(InsertionSort);
|
SortTester.testSortStability(InsertionSort);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should sort negative numbers', () => {
|
||||||
|
SortTester.testNegativeNumbersSort(InsertionSort);
|
||||||
|
});
|
||||||
|
|
||||||
it('should visit EQUAL array element specified number of times', () => {
|
it('should visit EQUAL array element specified number of times', () => {
|
||||||
SortTester.testAlgorithmTimeComplexity(
|
SortTester.testAlgorithmTimeComplexity(
|
||||||
InsertionSort,
|
InsertionSort,
|
||||||
|
@ -26,6 +26,10 @@ describe('MergeSort', () => {
|
|||||||
SortTester.testSortStability(MergeSort);
|
SortTester.testSortStability(MergeSort);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should sort negative numbers', () => {
|
||||||
|
SortTester.testNegativeNumbersSort(MergeSort);
|
||||||
|
});
|
||||||
|
|
||||||
it('should visit EQUAL array element specified number of times', () => {
|
it('should visit EQUAL array element specified number of times', () => {
|
||||||
SortTester.testAlgorithmTimeComplexity(
|
SortTester.testAlgorithmTimeComplexity(
|
||||||
MergeSort,
|
MergeSort,
|
||||||
|
@ -26,6 +26,10 @@ describe('QuickSort', () => {
|
|||||||
SortTester.testSortStability(QuickSort);
|
SortTester.testSortStability(QuickSort);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should sort negative numbers', () => {
|
||||||
|
SortTester.testNegativeNumbersSort(QuickSort);
|
||||||
|
});
|
||||||
|
|
||||||
it('should visit EQUAL array element specified number of times', () => {
|
it('should visit EQUAL array element specified number of times', () => {
|
||||||
SortTester.testAlgorithmTimeComplexity(
|
SortTester.testAlgorithmTimeComplexity(
|
||||||
QuickSort,
|
QuickSort,
|
||||||
|
@ -22,6 +22,10 @@ describe('QuickSortInPlace', () => {
|
|||||||
SortTester.testSortWithCustomComparator(QuickSortInPlace);
|
SortTester.testSortWithCustomComparator(QuickSortInPlace);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should sort negative numbers', () => {
|
||||||
|
SortTester.testNegativeNumbersSort(QuickSortInPlace);
|
||||||
|
});
|
||||||
|
|
||||||
it('should visit EQUAL array element specified number of times', () => {
|
it('should visit EQUAL array element specified number of times', () => {
|
||||||
SortTester.testAlgorithmTimeComplexity(
|
SortTester.testAlgorithmTimeComplexity(
|
||||||
QuickSortInPlace,
|
QuickSortInPlace,
|
||||||
|
@ -22,6 +22,10 @@ describe('SelectionSort', () => {
|
|||||||
SortTester.testSortWithCustomComparator(SelectionSort);
|
SortTester.testSortWithCustomComparator(SelectionSort);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should sort negative numbers', () => {
|
||||||
|
SortTester.testNegativeNumbersSort(SelectionSort);
|
||||||
|
});
|
||||||
|
|
||||||
it('should visit EQUAL array element specified number of times', () => {
|
it('should visit EQUAL array element specified number of times', () => {
|
||||||
SortTester.testAlgorithmTimeComplexity(
|
SortTester.testAlgorithmTimeComplexity(
|
||||||
SelectionSort,
|
SelectionSort,
|
||||||
|
@ -22,6 +22,10 @@ describe('ShellSort', () => {
|
|||||||
SortTester.testSortWithCustomComparator(ShellSort);
|
SortTester.testSortWithCustomComparator(ShellSort);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should sort negative numbers', () => {
|
||||||
|
SortTester.testNegativeNumbersSort(ShellSort);
|
||||||
|
});
|
||||||
|
|
||||||
it('should visit EQUAL array element specified number of times', () => {
|
it('should visit EQUAL array element specified number of times', () => {
|
||||||
SortTester.testAlgorithmTimeComplexity(
|
SortTester.testAlgorithmTimeComplexity(
|
||||||
ShellSort,
|
ShellSort,
|
||||||
|
Loading…
Reference in New Issue
Block a user