-
Notifications
You must be signed in to change notification settings - Fork 26
/
快速排序.html
42 lines (33 loc) · 838 Bytes
/
快速排序.html
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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset=utf-8>
<title></title>
</head>
<style>
</style>
<body>
<script>
function quickSort(arr){
if(arr.length<=1){
return arr;
}
var num = Math.floor(arr.length/2);//中间位置索引,用做标记
var numValue = arr.splice(num,1);//取出标记位置的值
var left = [];
var right = [];
for(var i =0;i<arr.length;i++){
if(arr[i]<numValue){
left.push(arr[i]);
}else{
right.push(arr[i]);
}
}
return quickSort(left).concat([numValue],quickSort(right));
}
var arr = [12,5,37,6,22,40];
//alert(quickSort(arr));
console.log(quickSort(arr));
</script>
</body>
</html>