-
Notifications
You must be signed in to change notification settings - Fork 0
/
allFiveSorts.js
90 lines (71 loc) · 1.68 KB
/
allFiveSorts.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function bubble(array){
for(let i=0;i<array.length-1;i++){
for(let j=i+1;j<array.length;j++){
if(array[i]>array[j]){
[array[i],array[j]]=[array[j],array[i]]
}
}
}return array
}
function insertion(array){
for(let i=1;i<array.length;i++){
j=i-1
let current=array[i]
while(j>=0&&array[j]>current){
array[j+1]=array[j]
j--
}
array[j+1]=current
}return array
}
function select(array){
for(let i=0;i<array.length;i++){
let min=i
for(let j=i+1;j<array.length;j++){
if(array[j]<array[min]){
min=j
}
}
if(min!=i){
[array[i],array[min]]=[array[min],array[i]]
}
}return array
}
function quick(array){
if(array.length<=1){
return array
}
let pivot=array[0]
let left=[]
let right=[]
for(let i=1;i<array.length;i++){
if(array[i]<pivot){
left.push(array[i])
}else{
right.push(array[i])
}
}
return([...quick(left),pivot,...quick(right)])
}
function merge(array){
if(array.length<=1){
return array
}
let middle=array.length/2
let left=array.slice(0,middle)
let right=array.slice(middle)
return merge2(merge(left),merge(right))
}
function merge2(left,right){
let newArray=[]
while(left.length&&right.length){
if(left[0]<right[0]){
newArray.push(left.shift())
}else{
newArray.push(right.shift())
}
}return newArray.concat(left,right)
}
let array=[5,4,8,6,9,3,2,1]
let res=merge(array)
console.log(res);