We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7b23fcb commit 24d5ce5Copy full SHA for 24d5ce5
6.quickSort.md
@@ -30,6 +30,24 @@
30
## 3. JavaScript 代码实现
31
32
```js
33
+// NO.1 Solution
34
+var quickSort = function(arr) {
35
+ if (arr.length <= 1) { return arr; }
36
+ var pivotIndex = Math.floor(arr.length / 2); //基准位置(理论上可任意选取)
37
+ var pivot = arr.splice(pivotIndex, 1)[0]; //基准数
38
+ var left = [];
39
+ var right = [];
40
+ for (var i = 0; i < arr.length; i++){
41
+ if (arr[i] < pivot) {
42
+ left.push(arr[i]);
43
+ } else {
44
+ right.push(arr[i]);
45
+ }
46
47
+ return quickSort(left).concat([pivot], quickSort(right)); //链接左数组、基准数构成的数组、右数组
48
+};
49
+
50
+// NO.2 Solution
51
function quickSort(arr, left, right) {
52
var len = arr.length,
53
partitionIndex,
0 commit comments