-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-08.js
69 lines (47 loc) · 1.04 KB
/
test-08.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
console.log("-----------");
let div = document.createElement('div');
div.innerHTML = 'behippo';
document.body.appendChild(div);
let arr = [];
for(let i = 0; i < 5 ;i ++){
arr.push(Math.floor(Math.random()*9) + 1);
}
console.log(arr);
// 冒泡排序
for(let i = 0, len = arr.length; i < len; i ++){
for(let j = i + 1; j < len; j ++){
if(arr[i] > arr[j]){
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
}
// 选择排序
let min_index = Infinity;
for(let i = 0,len = arr.length; i < len; i++){
min_index = i;
for(let j = i + 1; j < len; j++){
if(arr[min_index] > arr[j]){
min_index = j;
}
}
[arr[min_index], arr[i]] = [arr[i], arr[min_index]]
}
console.log(arr);
// 快速排序
function quickSort(arr){
if(arr.length <= 1){
return arr;
}
let left = [];
let right = [];
let pivot_index = Math.floor(arr.length / 2);
let pivot = arr.splice(pivot_index, 1)[0];
arr.forEach((item) => {
if(item < pivot){
left.push(item);
}else{
right.push(item);
}
})
return quickSort(left).concat(pivot, quickSort(right));
}