排序算法&深浅拷贝&Promise()代码实现

2021-08-11  本文已影响0人  大佬教我写程序

冒泡排序

image.png
function bubbleStore(arr) {
  for (let i = 0; i < arr.length; i++) {
    //之所以减一,是因为比较的次数是总数的减一
    for (let j = 0; j < arr.length - i - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        let temp = arr[j + 1]
        arr[j + 1] = arr[j]
        arr[j] = temp
      }
    }
  }
  return arr
}
let arr = [1, 22, 0, 1, 8, 4, 2, 1, 445, 2, 56, 41, 6, 11, 5, 1]
let result = bubbleStore(arr)
console.log(result)

选择排序

function SelectSort(arr) {
  let mainIndex
  //最后一个元素不用比较
  for (let i = 0; i < arr.length - 1; i++) {
    //最小元素下标0,1,2,3,4,5,...
    mainIndex = i
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[j] < arr[mainIndex]) {
        //找到最小元素的下标
        mainIndex = j
      }
    }
    //将遍历出来的最小元素和前面的交换数值
    let temp = arr[i]
    arr[i] = arr[mainIndex]
    arr[mainIndex] = temp
  }
  return arr
}
let arr = [1, 22, 0, 1, 8, 4, 2, 1, 445, 2, 56, 41, 6, 11, 5, 1]
let result = SelectSort(arr)
console.log(result)

插入排序

image
  function insertionSort(arr) {
        var len = arr.length;
        var preIndex, current;
        for (var i = 1; i < len; i++) {
          preIndex = i - 1;
          current = arr[i];
          while (preIndex >= 0 && arr[preIndex] > current) {
            arr[preIndex + 1] = arr[preIndex];
            preIndex--;
          }
          arr[preIndex + 1] = current;
        }
        return arr;
      }

浅拷贝


        // 第一种:
        var obj1 = {
            age: 10,
            sex: "男",
            car: ["奔驰", "宝马", "特斯拉", "奥拓"]
        };
        //另一个对象
        var obj2 = {};
 
        //写一个函数,作用:把一个对象的属性复制到另一个对象中,浅拷贝
        //把a对象中的所有的属性复制到对象b中
        function shallowCopy(obj,targetObj){
            for (let key in obj){
                targetObj[key] = obj[key];
            }
        }
        shallowCopy(obj1, obj2);
        console.dir(obj2);//开始的时候这个对象是空对象
        console.dir(obj1);//有属性
        //change car attribute
        obj1.car.push("奥迪");
        //the car of obj2 change,too.for the point of the car in obj2 is same as the obj1
        console.log(obj2.car);
 
        // 第二种
        var obj3 = obj1;
        console.dir(obj3)

深拷贝

        var obj1 = {
            age: 10,
            sex: "男",
            car: ["奔驰", "宝马", "特斯拉", "奥拓"],
            dog: {
                name: "大黄",
                age: 5,
                color: "黑白色"
            }
        };
 
        var obj2 = {};//空对象
        //通过函数实现,把对象a中的所有的数据深拷贝到对象b中
        // use recursion
        function deepCopy(obj,targetObj){
            for (let key in obj){
                let item = obj[key];
                if (item instanceof Array){//if array
                    targetObj[key] = [];
                    deepCopy(item,targetObj[key]);
                }else if (item instanceof Object){//if object
                    targetObj[key] = {};
                    deepCopy(item,targetObj[key]);
                }else {//normal attribute
                    targetObj[key] = obj[key];
                }
            }
        }
        deepCopy(obj1,obj2);
        console.dir(obj1);
        console.dir(obj2);

Promise()函数代码实现

class PromiseM {
  constructor(process) {
    this.status = 'pending'
    this.msg = ''
    process(this.resolve.bind(this), this.reject.bind(this))
    return this
  }
  resolve(val) {
    this.status = 'fulfilled'
    this.msg = val
  }
  reject(err) {
    this.status = 'rejected'
    this.msg = err
  }
  then(fufilled, reject) {
    if (this.status === 'fulfilled') {
      fufilled(this.msg)
    }
    if (this.status === 'rejected') {
      reject(this.msg)
    }
  }
}


//测试
var mm = new PromiseM(function (resolve, reject) {
  resolve('123')
})
mm.then(
  function (success) {
    console.log(success)
  },
  function () {
    console.log('fail!')
  }
)

上一篇下一篇

猜你喜欢

热点阅读