Remove quickSort
This commit is contained in:
@ -1,26 +0,0 @@
|
||||
---
|
||||
title: quickSort
|
||||
tags: algorithm,recursion,beginner
|
||||
---
|
||||
|
||||
QuickSort an Array (ascending sort by default).
|
||||
|
||||
Use recursion.
|
||||
Use `Array.prototype.filter` and spread operator (`...`) to create an array that all elements with values less than the pivot come before the pivot, and all elements with values greater than the pivot come after it.
|
||||
If the parameter `desc` is truthy, return array sorts in descending order.
|
||||
|
||||
```js
|
||||
const quickSort = ([n, ...nums], desc) =>
|
||||
isNaN(n)
|
||||
? []
|
||||
: [
|
||||
...quickSort(nums.filter(v => (desc ? v > n : v <= n)), desc),
|
||||
n,
|
||||
...quickSort(nums.filter(v => (!desc ? v > n : v <= n)), desc)
|
||||
];
|
||||
```
|
||||
|
||||
```js
|
||||
quickSort([4, 1, 3, 2]); // [1,2,3,4]
|
||||
quickSort([4, 1, 3, 2], true); // [4,3,2,1]
|
||||
```
|
||||
@ -1,42 +0,0 @@
|
||||
const {quickSort} = require('./_30s.js');
|
||||
|
||||
test('quickSort is a Function', () => {
|
||||
expect(quickSort).toBeInstanceOf(Function);
|
||||
});
|
||||
test('quickSort([5, 6, 4, 3, 1, 2]) returns [1, 2, 3, 4, 5, 6]', () => {
|
||||
expect(quickSort([5, 6, 4, 3, 1, 2])).toEqual([1, 2, 3, 4, 5, 6]);
|
||||
});
|
||||
test('quickSort([-1, 0, -2]) returns [-2, -1, 0]', () => {
|
||||
expect(quickSort([-1, 0, -2])).toEqual([-2, -1, 0]);
|
||||
});
|
||||
test('quickSort() throws an error', () => {
|
||||
expect(() => {
|
||||
quickSort();
|
||||
}).toThrow();
|
||||
});
|
||||
test('quickSort(123) throws an error', () => {
|
||||
expect(() => {
|
||||
quickSort(123);
|
||||
}).toThrow();
|
||||
});
|
||||
test('quickSort({ 234: string}) throws an error', () => {
|
||||
expect(() => {
|
||||
quickSort({ 234: string });
|
||||
}).toThrow();
|
||||
});
|
||||
test('quickSort(null) throws an error', () => {
|
||||
expect(() => {
|
||||
quickSort(null);
|
||||
}).toThrow();
|
||||
});
|
||||
test('quickSort(undefined) throws an error', () => {
|
||||
expect(() => {
|
||||
quickSort(undefined);
|
||||
}).toThrow();
|
||||
});
|
||||
let start = new Date().getTime();
|
||||
quickSort([11, 1, 324, 23232, -1, 53, 2, 524, 32, 13, 156, 133, 62, 12, 4]);
|
||||
let end = new Date().getTime();
|
||||
test('quickSort([11, 1, 324, 23232, -1, 53, 2, 524, 32, 13, 156, 133, 62, 12, 4]) takes less than 2s to run', () => {
|
||||
expect(end - start < 2000).toBeTruthy();
|
||||
});
|
||||
Reference in New Issue
Block a user