<!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 selectionSotr(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[minIndex], arr[i]] = [arr[i], arr[minIndex]]; } return 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)); } // 回文数 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; } function isPlander2(x) { return String(x).split('').reverse().join('') === String(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 throttle1(func, delay) { let old = 0; return function () { let now = Date.now(); if (now - old > delay) { func.apply(this.arguments); } }; } function throttle2(func, delay) { let timer = null; return function () { let context = this; let args = arguments; if (!timer) { timer = setTimeout(function () { func.apply(context, args); timer = null; }, delay); } }; } // 柯里化 function add() { let _args = [...arguments]; let _adder = function () { _args.push(...arguments); return _adder; }; _adder.toString = function () { return _args.reduce((total, cur) => { return total + cur; }, 0); }; return _adder; } // 深拷贝 function deepCopy(obj) { let result = Array.isArray(obj) ? [] : {}; for (let key in obj) { if (obj.hasOwnPorperty(key)) { if (typeof obj[key] === 'object' && obj[key] !== null) { result[ley] = deepCopy(obj[key]); } else { result[key] = obj[key]; } } } return rsult; } // 数组去重 function removeDuplication(arr) { return [...new Set(arr)]; } // 二分法 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; } let arr = [1, 2, 3, 4, 3, 2, 1]; let x = 12321; let arr2 = [1, 2, 3, 4, 5, 6]; console.log(bubbleSort(arr)); console.log(searching(arr2, 6)); console.log(selectionSotr(arr)); console.log(quickSort(arr)); console.log(isPlander(12321)); console.log(isPlander2(12321)); console.log(add(1)(2)(3)); console.log(add(1, 2, 3)(4)); </script> </body> </html>
真的是 越来越熟练~ 一起加油!
争取早日写上 leetCode~