<!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 - 1; 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; } // 选择排序 function selectionSort(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; } // 交换 格式写错 忘记写 return 语句 // 快速排序 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)); } // 二分法查找 function searchingSort(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; } // 回文数 function ispalient(num) { if (num < 0) return false; return String(num).split('').reverse.join('') == num; } // 回文数忘记了 function ispalient2(num) { if (num < 0) return false; let d = 0; let f = num; while (f > 0) { d = d + (f % 10); f = (f - (f % 10)) / 10; } return d === num; } function isPalindrome2(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; } // 防抖 function debounce(func, delay) { let timer; return function () { clearTimeout(timer); let context = this; let args = arguments; // 这里有疑问,是不是该转化为数组 timer = setTimeout(function () { func.apply(context, args); }, delay); }; } // 节流 不会写了 function throttle(func, delay) { let old = 0; return function () { let now = Date.now(); if (now - old > delay) { func.apply(context, args); } }; } // 深拷贝 function deepCopy(obj) { let result = Array.isArray(obj) ? [] : {}; for (let key in obj) { if (obj.hasOwnproperty(key)) { if (typeof obj[key] === 'obj' && obj[key] !== null) { result[key] = deepCopy(obj[key]); } else { result[key] = obj[key]; } } } return result; } async function A() { let a = await setInterval(function () { return 113; }, 2000); let b = a; console.log(b); } function* B(x) { yield x + 1; yield x + 2; yield x + 3; yield x + 4; } // 扁平化处理 function flat(arr) { return arr.reduce((total, cur) => { return total.concat(Array.isArray(cur) ? flat(cur) : cur); }, []); } function* C(arr) { for (let i = 0; i < arr.length - 1; i++) { yield arr[i]; } } // 生成器不太会用 // 函数柯里化 function add() { let _args = [...arguments]; let _adder = function () { _args.push(...arguments); return _adder; }; _adder.toString = function () { return _args.reduce((total, cur) => total + cur, 0); }; return _adder; } console.log(add(1)(2)(3)); console.log(add(1, 2, 3)(4, 5)); console.log(bubbleSort([2, 1, 2, 3])); console.log(quickSort([2, 1, 2, 3])); console.log(selectionSort([2, 1, 3, 4])); console.log(searchingSort([1, 2, 3, 4, 5], 5)); </script> </body> </html>
基本掌握了 开始下一步学习~