MCPcopy
hub / github.com/HuberTRoy/leetCode / sort

Function sort

Array/KthLargestElementInAnArray.js:8–34  ·  view source on GitHub ↗
(arr)

Source from the content-addressed store, hash-verified

6
7// 写了个快排和归并。
8 function sort(arr) {
9 // 快排
10 if (arr.length === 0) {
11 return arr
12 }
13
14 // 选种
15 let bean = Math.floor((arr.length-1) / 2)
16 let base = arr[bean]
17 let bigger = []
18 let smaller = []
19
20 for (let [index, item] of arr.entries()) {
21 if (index === bean) {
22 continue
23 }
24
25 if (item >= base) {
26 bigger.push(item)
27 } else {
28 smaller.push(item)
29 }
30 }
31
32 return [...sort(smaller), base, ...sort(bigger)]
33
34}
35
36// 归并
37function split(arr) {

Callers 1

sort2Function · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected