js罕見算法(一):排序,數組去重,打亂數組,統計數組各個元素湧現的次數, 字符串各個字符的湧現次數,獵取地點鏈接的各個參數

排序,數組去重,打亂數組,統計數組各個元素湧現的次數, 字符串各個字符的湧現次數,獵取地點鏈接的各個參數

以後會紀錄本身處理過和遇到過的算法相干的題,系列一就以罕見的開篇吧。

排序

本來想多列幾個排序要領,然則別的都不罕見,更多請點擊檢察,冒泡排序和疾速排序中以疾速排序最優。

1. 冒泡排序

var arr = [0, 4, 2, 1, 3];
var i = j = 0;
for(i; i<arr.length-1; i++){
    for(j; j<=arr.length-i; j++){
        var temp = 0;
        // ">" 從小到大排序
        // "<" 從大到小排序
        if(arr[j] > arr[j+1]){
            temp = arr[j]
            arr[j] = arr[j+1]
            arr[j+1] = temp
        }
    }
}

console.log(arr);

2. 疾速排序

function quickSort(arr){
    //假如數組<=1,則直接返回
    if (arr.length <= 1) { return arr; }
    var pivotIndex = Math.floor(arr.length/2);
    //找基準,並把基準從原數組刪除
    var pivot = arr.splice(pivotIndex,1)[0];
    //定義擺布數組
    var left = [];
    var right = [];

    //比基準小的放在left,比基準大的放在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), pivot, ...quickSort(right)];
} 

console.log(quickSort([0, 4, 2, 1, 3]));

數組去重

1. 遍曆數組法

var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
for (var i = 0, newArr = [], len = arr.length; i < len; i++){
    if (newArr.indexOf(arr[i]) == -1) {
        newArr.push(arr[i]);
    }
}
console.log(newArr);

2. 數組下標推斷法

var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
for (var i = 1, newArr = [arr[0]], len = arr.length; i < len; i++){
    if (arr.indexOf(arr[i]) == i) {
        newArr.push(arr[i]);
    }
}
console.log(newArr);

3. 對象鍵值對法

var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
for (var i = 0, obj = {}, newArr = [], len = arr.length, val, type; i < len; i++){
    val = arr[i];
    type = typeof val;
    if (!obj[val]) {  // 對象沒有該鍵對應的值
        obj[val] = type;
        newArr.push(val);
    } else if (obj[val].indexOf(type) < 0) {//對象有該鍵對應的值且它的值是不反覆的
        obj[val] = type;
        newArr.push(val);
    }
}
console.log(newArr);

4. 排序后相鄰去除法

var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
arr.sort();
var newArr = [arr[0]];
for (var i = 1, len = arr.length; i < len; i++) {
    if (arr[i] !== newArr[newArr.length-1]) {
        newArr.push(arr[i]);
    }
}
console.log(newArr);

5. 優化遍曆數組法

var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
for (var i = 0, len = arr.length, newArr = []; i < len; i++) {
    for (var j = i + 1; j < len; j++) {
        if (arr[i] === arr[j]) j = ++i;
    }
    newArr.push(arr[i]);
}
console.log(newArr);

6. 應用splice直接在原數組舉行操縱

var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
for (var i = 0, len = arr.length; i < len; i++) {
    for (var j = i + 1; j < len; j++) {
        if (arr[i] === arr[j]) {
            arr.splice(j, 1);
            len--;
            j--;
        }
    }
}
console.log(arr);

7. es6簡化版

var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
// 7.1
var newArr = [...new Set(arr)]; 
console.log(newArr);

// 7.2
var newArr = Array.from(new Set(arr));
console.log(newArr);
// 數組去重,打亂數組,統計數組各個元素湧現的次數, 字符串各個字符的湧現次數,獵取地點鏈接的各個參數

打亂數組

var arr = [2,5,'6',3,9,'2',4,7];
arr.sort(function(){ return 0.5 - Math.random() });
console.log(arr);

統計數組各個元素湧現的次數

1. 自創

var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
for (var i = 0, len = arr.length, newArr = []; i < len; i++) {
    var isPush = true;
    for (var j = 0, l = newArr.length; j < l; j++) {
        if (arr[i] === newArr[j].val) {
            isPush = false;
            newArr[j].count++;
        }
    }
    if (isPush) {
        newArr.push({val: arr[i], count: 1});
    } else {
        isPush = true;
    }
}
console.log(newArr);

2. es6

var arr = ['2', 1, 5, 4, 7, '9', 4, 1, '2', 6, 8, 2];
var newArr = arr.reduce((arrs, cv) => {
    const found = arrs.find(it => it[0] === cv);
    if (found) {
        found[1] += 1;
    } else {
        arrs.push([cv, 1]);
    }
    return arrs;
}, []);
console.log(newArr);

字符串各個字符的湧現次數

var str = 'abdcsdwdaoprr';
for (var i = 0, len = str.length, obj = {}; i < len; i++) {
    if (obj[str[i]]) {
        obj[str[i]]++;
    } else {
        obj[str[i]] = 1;
    }
}
console.log(obj);

獵取地點鏈接的各個參數

var url = 'https://www.google.com.hk/search?safe=strict&ei=M_u5WvLZO4bzUrfpn_AJ&q=js%9F%&oq=js%E7%BB&gs_l=psy-ab';
var search = url.substring(url.indexOf('?')+1);
    var tmpArr = search.split('&');
    var urlParmObj = {};
    if (tmpArr.length > 0 && tmpArr[0] != '') {
            for (var i = 0, len = tmpArr.length; i < len; i++) {
                    var tmp = tmpArr[i].split('=');
                    urlParmObj[tmp[0]] = tmp[1];
            }
    }
    console.log(urlParmObj);

es6 實踐

var arr = [1,2,3,4,5];
const param = arr.find(it => it === 5);
param += 1;
console.log(arr);

更多別的內容請檢察

    原文作者:xingqiwu
    原文地址: https://segmentfault.com/a/1190000015068574
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞