面试视频知识点整理1-11(算法类)

1、排序类

快速排序:https://segmentfault.com/a/1190000009426421

var quickSort = function(arr) {
    if (arr.length <= 1) { return arr; }
    var pivotIndex = Math.floor(arr.length / 2);   //基准位置(理论上可任意选取)
    var pivot = arr.splice(pivotIndex, 1)[0];  //基准数
    var left = [];
    var right = [];
    for (var i = 0; i < arr.length; i++){
        if (arr[i] < pivot) {
            left.push(arr[i]);
        } else {
            right.push(arr[i]);
        }
    }
    return quickSort(left).concat([pivot], quickSort(right));  //链接左数组、基准数构成的数组、右数组
};
View Code

选择排序:https://segmentfault.com/a/1190000009366805

function selectionSort(arr) {
    var len = arr.length;
    var minIndex, temp;
    for (var i = 0; i < len - 1; i++) {
        minIndex = i;
        for (var j = i + 1; j < len; j++) {
            if (arr[j] < arr[minIndex]) {     // 寻找最小的数
                minIndex = j;                 // 将最小数的索引保存
            }
        }
        temp = arr[i];
        arr[i] = arr[minIndex];
        arr[minIndex] = temp;
    }
    return arr;
}
View Code

希尔排序:https://segmentfault.com/a/1190000009461832

function shellSort(arr) {
    var len = arr.length,
        temp,
        gap = 1;
    while(gap < len/3) {          //动态定义间隔序列
        gap = gap*3+1;
    }
    for (gap; gap > 0; gap = Math.floor(gap/3)) {
        for (var i = gap; i < len; i++) {
            temp = arr[i];
            for (var j = i-gap; j >= 0 && arr[j] > temp; j -= gap) {
                arr[j+gap] = arr[j];
            }
            arr[j+gap] = temp;
        }
    }
    return arr;
}
View Code

 

2、堆栈,队列、链表

堆栈:https://juejin.im/entry/58759e79128fe1006b48cdfd

队列:https://juejin.im/entry/58759e79128fe1006b48cdfd

链表:https://juejin.im/entry/58759e79128fe1006b48cdfd

 

3、递归

递归:https://segmentfault.com/a/1190000009857470

 

4、波兰式和逆波兰式

理论:http:www.cnblogs.com/chenying99/p/3675876.html

源码:https://github.com/Tairraos/rpn.js/blob/master/rpn.js

 

posted @ 2020-03-01 10:22  LLC-Mite  阅读(193)  评论(0编辑  收藏  举报