寒冬期前端准备总结---算法篇

⚠️⚠️传送门⚠️⚠️

寒冬期前端准备总结---JS篇
寒冬期前端准备总结---浏览器篇
寒冬期前端准备总结---服务器和网络篇
寒冬期前端准备总结---CSS篇
寒冬期前端准备总结---框架篇
[寒冬期前端准备总结---算法篇]

  • 排序算法
    冒泡排序:O2(第一位和第二位比较,第二位和第三位比较)
    选择排序:O2(每次遍历数组找到最小值,放在数组前面)
    插入排序:O2(每次遍历比较在前面数组的位置,并插入到新的位置)
    快速排序:复杂度nlogn(找到中间值,左右放置,递归,每次比较结束数组长度-1)
  • 二叉树
深度遍历(前序、中序、后序):递归和非递归实现
广度遍历:递归和非递归实现

深度遍历
// 递归
var result = [];
var dfs1 = function(nodes) {
  if(nodes.node) {
    result.push(nodes.node);
    nodes.left && dfs1(nodes.left);
    nodes.right && dfs1(nodes.right);
  }
}
dfs(nodes);

// 非递归
var dfs2 = function(nodes) {
  var result = [];
  var stack = [];
  stack.push(nodes);
  while (stack.length) {
    var item = stack.pop();
    result.push(item.node);
    item.right && stack.push(item.right);
    item.left && stack.push(item.left);
  }
  return result;
}

广度遍历
var bfs = function(nodes) {
  var result = []
  var queue = []
  var pointer = 0
  queue.push(nodes)
  while(pointer < queue.length) {
    var item = queue[pointer++] // 这里不使用 shift 方法(复杂度高),用一个指针代替
    result.push(item.node);
    item.left && queue.push(item.left);
    item.right && queue.push(item.right);
  }
  return result;
}
  • 冒泡排序
function Bubble(arr) {
    for (let i = 0; i < arr.length - 1; i++) {
        for (let j = i + 1; j < arr.length - 1; j++) {
            if (arr[i] > arr[j]) {
                let temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return arr;
}
  • 快速排序
function quickSort(arr) {
    if (arr.length <= 1) return arr;
    let arrLeft = [], arrRight = [];
    let mid = arr[0];
    for (let i = 1; i < arr.length - 1; i++) {
        if (arr[i] <= mid) { arrLeft.push(arr[i]); }
        if (arr[i] > mid) { arrRight.push(arr[i]); }
    }
    return [].concat(quickSort(arrLeft), [mid], quickSort(arrRight));
}
  • promise、async实现循环打印1-10
function count(num) {
    return new Promise((resolve) => {
        setTimeout(() => {
            console.log('a', num);
            resolve(num);
        }, 1000);
    })
}
async function printNum() {
    for (let i = 1; i <= 10; i++) {
        await count(i);
        console.log('b', i);
    }
}
  • 手写Promise
function Promise(executor) {
    let self = this;
    self.status = 'pending';
    self.value = undefined;// 成功值
    self.errValue = undefined;// 失败值

    self.onResolved = [];// 用来保存then 方法中,第一个参数
    self.onRejected = [];// 用来保存then 方法中,第二个参数
    function resolve(value) {
        if (self.status === 'pending') {
            self.status = 'fulfilled';
            self.value = value;
            self.onResolved.forEach(fn => fn());
        }
    }
    function reject(value) {
        if (self.status === 'pending') {
            self.status = 'rejected';
            self.errValue = value;
            self.onRejected.forEach(fn => fn());
        }
    }
    try {
        executor(resolve, reject);
    } catch (e) {
        reject(e);
    }
}
Promise.prototype.then = function (onfulfilled, onrejected) {
    let self = this;
    return new Promise((resolve, reject) => {
        if (self.status === 'fulfilled') {
            resolve(onfulfilled(self.value));
        }
        if (self.status === 'rejected') {
            reject(onrejected(self.errValue));
        }
        if (self.status === 'pending') {
            self.onResolved.push(function () {
                try {
                    let x = onfulfilled(self.value);
                    resolve(x)
                } catch (e) {
                    reject(e)
                }
            })
            self.onRejected.push(function () {
                try {
                    let x = onrejected(self.errValue);
                    resolve(x)
                } catch (e) {
                    reject(e)
                }
            })
        }
    });
}
Promise.prototype.catch = function (onfulfilled, onrejected) {
    let self = this;
    return new Promise((resolve, reject) => reject(onrejected(self.errValue)));
}
let p = new Promise((resolve, reject) => {
    // resolve('111');
    setTimeout(function () {
        resolve('222');
    }, 500)
})
p.then((value) => {
    return value;
}, (reason) => {
    console.log('err1', reason);
}).then((data) => {
    console.log(data);
}, (reason) => {
    console.log('err2', reason);
})

你可能感兴趣的:(寒冬期前端准备总结---算法篇)