mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
Add declarative version of quick sort
This commit is contained in:
parent
94abfec91d
commit
7bdd956478
23
src/algorithms/sorting/quick-sort/QuickSortDeclarative.js
Normal file
23
src/algorithms/sorting/quick-sort/QuickSortDeclarative.js
Normal file
@ -0,0 +1,23 @@
|
||||
import Sort from '../Sort';
|
||||
|
||||
export default class QuickDeclarativeSort extends Sort {
|
||||
/**
|
||||
* @param {*[]} originalArray
|
||||
* @return {*[]}
|
||||
*/
|
||||
sort(originalArray) {
|
||||
if (originalArray.length < 1) {
|
||||
return originalArray;
|
||||
}
|
||||
const array = [...originalArray];
|
||||
const currentElement = array.shift();
|
||||
|
||||
this.callbacks.visitingCallback(currentElement);
|
||||
|
||||
return [
|
||||
...this.sort(array.filter(element => this.comparator.lessThan(element, currentElement))),
|
||||
currentElement,
|
||||
...this.sort(array.filter(element => !this.comparator.lessThan(element, currentElement))),
|
||||
];
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
import QuickSortDeclarative from '../QuickSortDeclarative';
|
||||
import {
|
||||
equalArr,
|
||||
notSortedArr,
|
||||
reverseArr,
|
||||
sortedArr,
|
||||
SortTester,
|
||||
} from '../../SortTester';
|
||||
|
||||
// Complexity constants.
|
||||
const SORTED_ARRAY_VISITING_COUNT = 20;
|
||||
const NOT_SORTED_ARRAY_VISITING_COUNT = 20;
|
||||
const REVERSE_SORTED_ARRAY_VISITING_COUNT = 20;
|
||||
const EQUAL_ARRAY_VISITING_COUNT = 20;
|
||||
|
||||
describe('QuickSortDeclarative', () => {
|
||||
it('should sort array', () => {
|
||||
SortTester.testSort(QuickSortDeclarative);
|
||||
});
|
||||
|
||||
it('should sort array with custom comparator', () => {
|
||||
SortTester.testSortWithCustomComparator(QuickSortDeclarative);
|
||||
});
|
||||
|
||||
it('should sort negative numbers', () => {
|
||||
SortTester.testNegativeNumbersSort(QuickSortDeclarative);
|
||||
});
|
||||
|
||||
it('should visit EQUAL array element specified number of times', () => {
|
||||
SortTester.testAlgorithmTimeComplexity(
|
||||
QuickSortDeclarative,
|
||||
equalArr,
|
||||
EQUAL_ARRAY_VISITING_COUNT,
|
||||
);
|
||||
});
|
||||
|
||||
it('should visit SORTED array element specified number of times', () => {
|
||||
SortTester.testAlgorithmTimeComplexity(
|
||||
QuickSortDeclarative,
|
||||
sortedArr,
|
||||
SORTED_ARRAY_VISITING_COUNT,
|
||||
);
|
||||
});
|
||||
|
||||
it('should visit NOT SORTED array element specified number of times', () => {
|
||||
SortTester.testAlgorithmTimeComplexity(
|
||||
QuickSortDeclarative,
|
||||
notSortedArr,
|
||||
NOT_SORTED_ARRAY_VISITING_COUNT,
|
||||
);
|
||||
});
|
||||
|
||||
it('should visit REVERSE SORTED array element specified number of times', () => {
|
||||
SortTester.testAlgorithmTimeComplexity(
|
||||
QuickSortDeclarative,
|
||||
reverseArr,
|
||||
REVERSE_SORTED_ARRAY_VISITING_COUNT,
|
||||
);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user