Skip to content

Commit 6c76f8b

Browse files
committed
快速排序
1 parent 29af762 commit 6c76f8b

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

快速排序.html

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,37 @@
1515
// 在数据集之中,选择一个元素作为"基准"(pivot)。
1616
// 所有小于"基准"的元素,都移到"基准"的左边;所有大于"基准"的元素,都移到"基准"的右边。
1717
// 对"基准"左边和右边的两个子集,不断重复第一步和第二步,直到所有子集只剩下一个元素为止。
18-
const quickSort = (arr) => { //传入一个数组
19-
if(arr.length <= 1) { //如果数组长度小于等于1无需判断直接返回即可
20-
return arr
21-
}
22-
let pivotIndex = Math.floor(arr.length / 2) //取基准点
23-
console.log(pivotIndex + " %%%")
24-
let pivot = arr.splice(pivotIndex, 1)[0] //取基准点的值,splice(index,1)函数可以返回数组中被删粗的那个数
25-
console.log(pivot + "))")
26-
let left = [] //存放比基准点小的数组
27-
let right = [] //存放比基准点大的数组
28-
for(var i = 0; i < arr.length; i++) { //遍历数组,进行判断分配
29-
if(arr[i] < pivot) {
30-
left.push(arr[i]) //比基准点小的放在左边数组
31-
} else {
32-
right.push(arr[i]) //比基准点大的放在右边数组
33-
}
34-
}
35-
//递归执行以上操作,对左右两个数组进行操作,直到数组长苏为《=1
36-
console.log(left + " " + right)
37-
return quickSort(left).concat([pivot], quickSort(right)) //使用递归,然后拼接,最好写到纸上一步一步,就会一目了然
38-
}
39-
console.log(quickSort([8, 7, 4, 1, 9, 2, 3])) //打印到控制台
40-
41-
18+
// const quickSort = (arr) => { //传入一个数组
19+
// if(arr.length <= 1) { //如果数组长度小于等于1无需判断直接返回即可
20+
// return arr
21+
// }
22+
// let pivotIndex = Math.floor(arr.length / 2) //取基准点
23+
// console.log(pivotIndex + " %%%")
24+
// let pivot = arr.splice(pivotIndex, 1)[0] //取基准点的值,splice(index,1)函数可以返回数组中被删粗的那个数
25+
// console.log(pivot + "))")
26+
// let left = [] //存放比基准点小的数组
27+
// let right = [] //存放比基准点大的数组
28+
// for(var i = 0; i < arr.length; i++) { //遍历数组,进行判断分配
29+
// if(arr[i] < pivot) {
30+
// left.push(arr[i]) //比基准点小的放在左边数组
31+
// } else {
32+
// right.push(arr[i]) //比基准点大的放在右边数组
33+
// }
34+
// }
35+
// //递归执行以上操作,对左右两个数组进行操作,直到数组长苏为《=1
36+
// console.log(left + " " + right)
37+
// return quickSort(left).concat([pivot], quickSort(right)) //使用递归,然后拼接,最好写到纸上一步一步,就会一目了然
38+
// }
39+
// console.log(quickSort([8, 7, 4, 1, 9, 2, 3])) //打印到控制台
40+
//
41+
if(typeof Array.isArray != "function") {
42+
Array.isArray = function(obj){
43+
return Object.prototype.toString.call(obj) == "[object Array]";
44+
console.log(111)
45+
}
46+
}
47+
Array.isArray([1,2,3])
48+
console.log(Array.isArray([1,2,3]))
4249
</script>
4350
</body>
4451

0 commit comments

Comments
 (0)