Skip to content

Commit c5b7a31

Browse files
author
mik-laj
committed
Add quick sort (functional variant)
1 parent 4f732ac commit c5b7a31

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
(function (exports) {
2+
3+
'use strict';
4+
5+
/**
6+
* The quicksort algorithm (functional variant). It's complexity is O(nlog n).
7+
*
8+
* @public
9+
*/
10+
var quickSort = (function () {
11+
12+
/**
13+
* Sorts given array.
14+
*
15+
* @private
16+
* @param {array} array Array which should be sorted
17+
* @returns {array} array Sorted array
18+
*/
19+
return function quickSort(array, left, right, cmp) {
20+
if ( arr.length < 1) {
21+
return arr;
22+
}
23+
24+
var [x, ...rest] = arr;
25+
26+
return [
27+
...quickSort(rest.filter(v => v <= x)),
28+
x,
29+
...quickSort(rest.filter(v => v > x))
30+
]
31+
return array;
32+
}
33+
34+
}());
35+
36+
exports.quickSort = quickSort;
37+
38+
}(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-functional.js').quickSort;
4+
5+
sortTestCase(quickSort, 'Quick sort', { reverse: false });

0 commit comments

Comments
 (0)