排序默写第四遍

<!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;
      }
      // 函数柯里化记得 有两个 return _adder
      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;
      }
      function isPlaind(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 isPlaind1(x) {
        return String(x).split('').reverse().join('') === String(x);
      }
      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[j] < arr[minIndex]) {
              minIndex = j;
            }
          }
          [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
        }
        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 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;
      }
      function flat(arr) {
        return arr.reduce((total, cur) => {
          return total.concat(Array.isArray(cur) ? flat(cur) : cur);
        }, []);
      }
      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] = deepCopy(obj[key]);
            } else {
              result[key] = obj[key];
            }
          }
        }
        return result;
      }
    </script>
  </body>
</html>

 

posted @ 2022-04-02 22:16  苹果π  阅读(10)  评论(0编辑  收藏  举报