首页 > 代码库 > 面试常见的js简单算法

面试常见的js简单算法

摘取于:

http://www.cnblogs.com/super-d2/archive/2011/10/16/2212865.html

https://segmentfault.com/a/1190000008593715

http://blog.csdn.net/owen1190/article/details/76215932

 

js算法一直是我的薄弱项,也不敢直面这惨淡,突然静下来,学了进去,记录一下:

快速排序,(可能导致堆栈溢出):

//快速排序
function quickSort(arr){
    if(arr.length <= 1){
        return arr;
    }
    var left = [], right = [];
    var midpos = Math.floor(arr.length/2),
    mid = arr.splice(mid, 1)[0];
    for(var i = 0; i < arr.length; i++){
        if(arr[i] < mid){
            left.push(arr[i]);
        }else{
            right.push(arr[i]);
        }
    }
    return quickSort(left).concat([mid], quickSort(right));
}
quickSort([12,54,1,25,67,41,12]);

 

插入排序:

//插入排序
function insert(arr){
    var i, 
        j, 
        tmp;
    for(i = 0; i < arr.length; i++){
        tmp = arr[i];
        for(j = i; j >= 0; j--){
            if(arr[j - 1] > tmp){
                arr[j] = arr[j - 1];
            }else{
                arr[j] = tmp;
                break;
            }
        }
    }
    return arr;
}
insert([12,54,1,25,67,41,12]);

 

选择排序:

//选择排序
function checked(arr){
    var i,
        j,
        tmp,
        minindex,
        minvalue;
    for(i = 0; i < arr.length - 1; i++){
        minindex = i;
        minvalue = arr[minindex];
        for(j = i + 1; j < arr.length; j++){
            if(arr[j] < minvalue ){
                minindex = j;
                minvalue = arr[minindex];
            }
        }
        tmp = arr[i];
        arr[i] = arr[minindex];
        arr[minindex] = tmp;
    }
    return arr;
}
checked([12,54,1,25,67,41,12]);

 

归并排序:

//归并排序
function meger(left, right){
    var tmp = [];
    while(left.length && right.length){
        if(left[0] < right[0]){
            tmp.push(left.shift());
        }else{
            tmp.push(right.shift());
        }
    }
    return tmp.concat(left, right);
}
function megerSort(arr){
    if(arr.length === 1){
        return arr;
    }
    var left = [], right = [], mid;
    mid = Math.floor(arr.length / 2);
    left = arr.slice(0, mid);
    right = arr.slice(mid);
    return meger(megerSort(left), megerSort(right));
}
megerSort([12,54,1,25,67,41,12]);

 

冒泡排序1:

//冒泡排序1
function maopao1(arr){
    var i,
        j,
        tmp;
    for(i = arr.length; i >= 0; i--){
        for(j = 0; j < i; j++){
            if(arr[j] < arr[j - 1]){
                tmp = arr[j - 1];
                arr[j - 1] = arr[j];
                arr[j] = tmp;
            }
        }
    }
    return arr;
}
maopao1([12,54,1,25,67,41,12]);

 

冒泡排序2:

function maopao2(arr){
    var i,
        j,
        tmp;
    for(i = 0; i < arr.length; i++){
        for(j = 0; j < arr.length - i; j++){
            if(arr[j] < arr[j - 1]){
                tmp = arr[j];
                arr[j]= arr[j - 1];
                arr[j - 1] = tmp;
            }
        }
    }
    return arr;
}
maopao2([12,54,1,25,67,41,12]);
//冒泡排序3
function bullSort(arr){
 var temp;
 for(var i = 0; i < arr.length; i++){
   for(var j = arr.length-1;j > i;j--){
     if(arr[j] < arr[j-1]){
       temp = arr[j];
       arr[j] = arr[j-1];
       arr[j - 1] = temp;
     }
   }
 }
 return arr;
}
bullSort([12,54,1,25,67,41,12]);

 

一些常见的应用:

①查找数组中出现次数最多的数字

//查找数组中出现次数最多的数字
function getCount(arr){
    var i,
        j,
        maxnum,
        maxvalue = 0,
        obj = {};
    for(i = 0; i < arr.length; i++){
        if(!obj[arr[i]]){
            obj[arr[i]] = 1;
        }else{
            obj[arr[i]] += 1;
        }
    }
    console.log(obj);
    for( j in obj){
        if(obj[j] > maxvalue){
            maxnum = j;
            maxvalue = obj[maxnum];
        }
    }
    return maxnum + " : " + maxvalue;
}
getCount([12,54,12,54,79,11,12]);

 

②数组中的最大最小值的差值

//数组中的最大最小值的差值
function maxMin(arr){
    var min = arr[0], max = arr[0];
    for(var i =0; i < arr.length; i++){
        if(arr[i] < min){
            min = arr[i];
        }
        if(arr[i] > max){
            max = arr[i];
        }
    }
    return max - min;
}
maxMin([12,54,79,84,11,52]);

 

③反转字符串

//反转字符串1
function reverseStr(str){
    var newstr = ‘‘;
    for(var i = str.length - 1; i >= 0; i--){
        newstr += str[i];
    }
    return newstr;
}
reverseStr(abcdefghijklmnopqrstuvwxyz0123456789);
//反转字符串2
function reverseStr(str){
    var newstr = str.split(‘‘), result = [], len = newstr.length;
    for(var i = 0; i < len; i++){
        result[(len - 1) - i] = newstr[i];
    }
    return result.join(‘‘);
}
reverseStr(abcdefghijklmnopqrstuvwxyz0123456789);
//反转字符串3
function reverseStr2(str){
    var newstr = str.split(‘‘);
        tmp,
        i = 0,
        j = newstr.length - 1;
    while(i < j){
        tmp = newstr[i];
        newstr[i] = newstr[j];
        newstr[j] = tmp;
    }
    return newstr.join(‘‘);
}
reverseStr(abcdefghijklmnopqrstuvwxyz0123456789);

 

④生成指定长度的字符串

//生成指定长度的字符串
function getStr(str, count){
    var newstr = ‘‘;
    for(var i = 0; i < count; i++){
        newstr += str.charAt(Math.floor(Math.random() * str.length));
    }
    return newstr;
}
getStr(abcdefghijklmnopqrstuvwxyz0123456789, 10);

 

⑤去重

//对象形式去重
function delMore(arr){
    var i, obj = {}, result = [];
    for(i = 0; i < arr.length; i++){
        if(!obj[arr[i]]){
            obj[arr[i]] = true;
            result.push(arr[i]);
        }
    }
    return result;
}
delMore([12,54,12,54,79,11,12]);
//选择排序方法去重
function insertMore(arr){
    var len = arr.length,
        newstr = [];
    for(var i = 0; i < len; i++){
        for(var j = i + 1; j < len; j++){
            if(arr[i] == arr[j]){
                ++i;
            }
        }
        newstr.push(arr[i]);
    }
    return newstr;
}
insertMore([12,54,12,54,79,11,12]);

 

面试常见的js简单算法