<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>排序默写最后一遍</title>
</head>
<body>
<script>
// 冒泡排序;
function bubbleSort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
let arr = [1, 2, 3, 2, 1];
// console.log(bubbleSort(arr));
// 选择排序;
function selection(arr) {
let minIndex;
for (let i = 0; i < arr.length - 1; i++) {
minIndex = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[minIndex] > arr[j]) {
minIndex = j;
}
}
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
}
return arr;
}
// console.log(selection(arr));
// 快速排序;
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
let left = [];
let right = [];
let middleIndex = Math.floor(arr.length / 2);
let middleValue = arr[middleIndex];
arr.splice(middleIndex, 1);
for (let i = 0; i < arr.length; i++) {
if (arr[i] < middleValue) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return quickSort(left).concat(middleValue, quickSort(right));
}
// console.log(quickSort(arr));
// 回文数;
function isPlander(x) {
if (x < 0) return false;
let d = 0;
let f = x;
while (f > 0) {
d = d * 10 + (f % 10);
f = (f - (f % 10)) / 10;
}
return d === x;
}
// console.log(isPlander(12321));
// console.log(isPlander(1231));
// 函数扁平化;
function flat(arr) {
return arr.reduce((total, cur) => {
return total.concat(Array.isArray(cur) ? flat(cur) : cur);
}, []);
}
// console.log(flat([1, 2, [3, 4, 5, [6, 7, 8]]]));
// 函数柯里化;
function add() {
let _args = [...arguments];
let _adder = function () {
_args.push(...arguments);
return _adder;
};
_adder.toString = function () {
return _args.reduce((total, cur) => total + cur);
};
return _adder;
}
// console.log(add(1, 2, 3));
// console.log(add(1)(2)(3));
// console.log(add(3, 4)(5));
// 防抖;
function debounce(func, delay) {
let timer;
return function () {
clearTimeout(timer);
let context = this;
let args = arguments;
timer = setTimeout(function () {
func.apply(context, args);
}, delay);
};
}
// 节流1;
function throttle1(func, delay) {
let old = 0;
return function () {
let now = Date.now();
if (now - old > delay) {
func.apply(this, arguments);
}
};
}
// 节流2;
function throttle2(func, delay) {
let timer = null;
return function () {
let context = this;
let args = arguments;
if (!timer) {
timer = setTimeout(function () {
func.apply(context, args);
}, delay);
}
};
}
// 深拷贝;
function deepCopy(obj) {
let result = Array.isArray(obj) ? [] : {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof Obj[key] === 'object' && obj[key] !== null) {
result[key] = obj[key];
} else {
result[key] = deepCopy(obj[key]);
}
}
}
return result;
}
// 二分法查找;
function searching(arr, target) {
let start = 0;
let end = arr.length - 1;
let middleIndex;
let middleValue;
while (start <= end) {
middleIndex = Math.floor((start + end) / 2);
middleValue = arr[middleIndex];
if (target === middleValue) {
return middleIndex;
} else if (target < middleValue) {
end = middleIndex - 1;
} else {
start = middleIndex + 1;
}
}
return -1;
}
console.log(searching([0, 1, 2, 3, 4, 5], 5));
console.log(searching([0, 1, 2, 3, 4, 5], 6));
</script>
</body>
</html>
告一段落,下一步进行知识点总结
前端兼容性问题;
性能优化问题;
key原理;