forked from utimur/algs_and_structures_course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5-quich-sort.js
60 lines (49 loc) · 1.2 KB
/
5-quich-sort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const arr = [0, 3, 2, 5, 6, 8, 23, 9, 4, 2, 1, 2, 9, 6, 4, 1, 7, -1, -5, 23, 6, 2, 35, 6, 3, 32, 9, 4, 2, 1, 2, 9, 6, 4, 1, 7, -1, -5, 23, 9, 4, 2, 1, 2, 9, 6, 4, 1, 7, -1, -5, 23,]
let count = 0
function quickSort(array) {
if (array.length <= 1) {
return array
}
const pivotIndex = Math.floor(array.length / 2)
const pivot = array[pivotIndex]
const less = []
const greater = []
for (let index = 0; index < array.length; index++) {
const element = array[index];
if (element === pivot) {
continue
}
if (element >= pivot) {
greater.push(element)
} else {
less.push(element)
}
}
return [...quickSort(less), pivot, ...quickSort(greater)]
}
console.log(quickSort([2, 1, 2, 9, 6, 4, 1]))
console.log('count', count)
let arr1 = [2, 1, 2, 9, 6, 4, 1]
let pivot = 9
let less = [2, 1, 2, 6, 4, 1]
let greater = [] // !!!!!!!!!!!!!!!
//less
arr1 = [2, 1, 2, 6, 4, 1]
pivot = 6
less = [2, 1, 2, 4, 1]
greater = []
//greater
arr1 = [2, 1, 2, 4, 1]
pivot = 2
less = [1, 1]
greater = [2, 4]
//less
arr1 = [1, 1]
pivot = 1
less = []
greater = [1]
//less
arr1 = [2, 4]
pivot = 4
less = []
greater = [4]