Skip to content

Commit 16214cd

Browse files
authored
Merge pull request mgechev#125 from mik-laj/master
Add quick sort (functional variant)
2 parents 0715aea + c9f659f commit 16214cd

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

src/sorting/quicksort-declarative.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
(function (exports) {
2+
3+
'use strict';
4+
5+
function compare(a, b) {
6+
return a - b;
7+
}
8+
9+
/**
10+
* Quicksort algorithm (declarative variant)
11+
*
12+
* @public
13+
* @param {array} array Array which should be sorted.
14+
* @return {array} Sorted array.
15+
*/
16+
var quickSort = (function () {
17+
18+
/**
19+
* Recursively calls itself.
20+
*
21+
* @private
22+
* @param {array} array Array which should be processed
23+
*/
24+
function quicksort(array, cmp) {
25+
if (array.length < 1) {
26+
return array;
27+
}
28+
29+
const [x, ...rest] = array;
30+
31+
return [
32+
...quicksort(rest.filter(v => cmp(v, x) < 0), cmp),
33+
x,
34+
...quicksort(rest.filter(v => cmp(v, x) >= 0), cmp)
35+
];
36+
}
37+
38+
39+
/**
40+
* Quicksort algorithm. In this version of quicksort used
41+
* declarative programming mechanisms.<br><br>
42+
* Time complexity: O(N log(N)).
43+
*
44+
* @example
45+
*
46+
* var sort = require('path-to-algorithms/src' +
47+
* '/sorting/quicksort-declarative').quickSort;
48+
* console.log(sort([2, 5, 1, 0, 4])); // [ 0, 1, 2, 4, 5 ]
49+
*
50+
* @public
51+
* @module sorting/quicksort-declarative
52+
* @param {Array} array Input array.
53+
* @param {Function} cmp Optional. A function that defines an
54+
* alternative sort order. The function should return a negative,
55+
* zero, or positive value, depending on the arguments.
56+
* @return {Array} Sorted array.
57+
*/
58+
return function (array, cmp) {
59+
cmp = cmp || compare;
60+
array = quicksort(array, cmp);
61+
return array;
62+
};
63+
64+
}());
65+
66+
exports.quickSort = quickSort;
67+
68+
}(typeof exports === 'undefined' ? window : exports));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var sortTestCase = require('./sort.testcase.js');
2+
var quickSort =
3+
require('../../src/sorting/quicksort-declarative.js').quickSort;
4+
5+
sortTestCase(quickSort, 'Quick sort');

test/sorting/sort.testcase.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = function (sort, algorithmName, options) {
3939
precision: 0
4040
});
4141
}
42-
sort(array);
42+
array = sort(array);
4343
for (var i = 0; i < array.length - 1; i += 1) {
4444
expect(array[i] <= array[i + 1]).toBeTruthy();
4545
}
@@ -53,7 +53,7 @@ module.exports = function (sort, algorithmName, options) {
5353
}
5454

5555
var array = createRandomArray();
56-
sort(array, comparator);
56+
array = sort(array, comparator);
5757

5858
for (var i = 0; i < array.length - 1; i += 1) {
5959
expect(array[i] >= array[i + 1]).toBeTruthy();

0 commit comments

Comments
 (0)