Javascript实现的常用算法(如冒泡、快速、鸽巢、奇偶等)


Posted in Javascript onApril 29, 2014
l = [6, 2, 4, 1, 98, 34, 5, 9, 23, 8, 10, 32]       
//冒泡排序
/*function bubbleSort(arr) {
    newarr = arr.slice()
    if (newarr.length <= 2) return newarr
    for (var i = 0; i < newarr.length - 1; i++) {
        for (var j = 0; j < newarr.length - i - 1; j++) {
            if (newarr[j] > newarr[j + 1]) {
                newarr[j] = [newarr[j + 1], newarr[j + 1] = newarr[j]][0]
            }
        }
    }
    return newarr
}
console.log(l)
result = bubbleSort(l)
console.log(result)*/
       
//快速排序
/*function quickSort(arr) {
    if (arr.length <= 1) return arr;
    var left = [],
        right = [],
        middle = [];
    var mid = arr[Math.floor(arr.length / 2)]
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] < mid) {
            left.push(arr[i]);
        } else if (arr[i] > mid) {
            right.push(arr[i]);
        } else {
            middle.push(mid);
        }
    }
    return [].concat(quickSort(left), middle, quickSort(right))
}
var results = quickSort(l)
console.log(results)
       
*/
//选择排序
/*function selectSort(arr) {
    var min = 9999,
        minindex = 0;
    var left = [],
        right = arr.slice();
    if (arr.length <= 1) return arr;
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] <= min) {
            min = arr[i];
            minindex = i;
        }
    }
    left.push(min);    
    right.splice(minindex, 1);
    return [].concat(left, selectSort(right))
}
       
var results = selectSort(l)
console.log(results)*/
       
//插入排序
/*function insertSort(arr) {
    var tmp;
    for (var i = 1; i < arr.length; i++) {
        tmp = arr[i];
        for (var j = i; j > 0; j--) {
            if (arr[j - 1] > tmp) {
                arr[j] = arr[j - 1];
            } else {
                break;
            }
        }
        arr[j] = tmp;
    }
    return arr;
}
var results = insertSort(l)
console.log(results)*/
//木桶排序
/*function bucketSort(arr){
    var bucket = [];
    var newarr = [];
    for (var i = 0; i < arr.length; i++) {
        bucket[arr[i]] = arr[i];
    }
    for (var i = 0; i < bucket.length; i++) {
        if (bucket[i] !== undefined){
            newarr.push(bucket[i])
        }
    }
    return newarr;
}
var results = bucketSort(l)
console.log(results)*/
//鸽巢排序,非负整数
/*function pigeonholeSort(arr){
    var tempArr = [];
    for(var i=0,l=arr.length;i<l;i++){
        tempArr[arr[i]] = (tempArr[arr[i]]+1)||1 ;
    }
    var result = [],count;
    for(var k=0;k<tempArr.length;k++){
        count = tempArr[k];
        if(count){
            for(var i=0;i<count;i++){
                result.push(k);
            }
        }      
    }
    return result;    
}
var results = pigeonholeSort(l)
console.log(results)*/
//归并排序
/*function mergeSort(arr) {
    if (arr.length <= 1) return arr;
    var mid = Math.floor(arr.length / 2);
    var left = MergeSort(arr.slice(0, mid));
    var right = MergeSort(arr.slice(mid));
    var result = [];
    while (left.length && right.length) {
        if (left[left.length - 1] <= right[0]) {
            result = result.concat(left);
            left = [];
        } else if (right[right.length - 1] < left[0]) {
            result = result.concat(right);
            right = [];
        } else {
            if (right[0] < left[0]) {
                result.push(right.shift());
            } else {
                result.push(left.shift());
            }
        }
    }
    result = result.concat(left, right);
    return result;
}*/
       
/*function mergeSort(arr) {
    var merge = function(left, right) {
        var result = [];
        while (left.length > 0 && right.length > 0) {
            if (left[0] < right[0]) {
                result.push(left.shift());
            } else {
                result.push(right.shift());
            }
        }
        return result.concat(left).concat(right);
    }
    if (arr.length == 1) return arr;
    var middle = Math.floor(arr.length / 2),
        left = arr.slice(0, middle),
        right = arr.slice(middle);
    return merge(mergeSort(left), mergeSort(right));
}
var results = mergeSort(l)
console.log(results)
*/
       
//堆排序
/*function heapSort(arr) {
    var findRoot = function(arr, p, length) {
        p = p || 0;
        length = length || arr.length;
        var self = arguments.callee;
        var l = p * 2 + 1;
        var r = (p + 1) * 2;
        var left, right;
        if (l < length) left = self(arr, l, length);
        if (r < length) right = self(arr, r, length);
        if (left > arr[p]) arr[p] = [left, arr[l] = arr[p]][0];
        if (right > arr[p]) arr[p] = [right, arr[r] = arr[p]][0];
        return arr[p];
    };
       
    for (var i = arr.length; i > 0; i--) {
        findRoot(arr, 0, i);
        arr[i - 1] = [arr[0], arr[0] = arr[i - 1]][0];
    }
    return arr;
}
var results = heapSort(l)
console.log(results)*/
       
//奇偶排列
/*function oddEvenSort(arr) {
    var swaped = true,
        k = 0;
    while (swaped) {
        if (k > 0) swaped = false;
        for (var i = k; i < arr.length - 1; i += 2) {
            if (arr[i]>arr[i+1]) {
                arr[i] = [ arr[i+1], arr[i+1]=arr[i] ][0];
                swaped = true;
            }
        }
        k = [1, 0][k]
    }
    return arr;
}
var results = oddEvenSort(l)
console.log(results)*/
function oddEvenSort(arr) {
    var swaped = true;
    while (swaped) {
        swaped = false;
        for (var i = 0; i < arr.length; i += 2) {
            if (arr[i] > arr[i + 1]) {
                arr[i] = [arr[i + 1], arr[i + 1] = arr[i]][0];
                swaped = true;
            }
        }
        for (var i = 1; i < arr.length; i += 2) {
            if (arr[i] > arr[i + 1]) {
                arr[i] = [arr[i + 1], arr[i + 1] = arr[i]][0];
                swaped = true;
            }
        }
    }
    return arr;
}
var results = oddEvenSort(l)
console.log(results)
Javascript 相关文章推荐
msn上的tab功能Firefox对childNodes处理的一个BUG
Jan 21 Javascript
关于JavaScript的一些看法
May 27 Javascript
Java框架SSH结合Easyui控件实现省市县三级联动示例解析
Jun 12 Javascript
通过jquery实现页面的动画效果(实例代码)
Sep 18 Javascript
JS对大量数据进行多重过滤的方法
Nov 04 Javascript
jQuery插件HighCharts绘制简单2D柱状图效果示例【附demo源码】
Mar 21 jQuery
jquery实现全选、全不选以及单选功能
Mar 23 jQuery
使用JavaScript开发跨平台的桌面应用详解
Jul 27 Javascript
vue中页面跳转拦截器的实现方法
Aug 23 Javascript
angularJs中$http获取后台数据的实例讲解
Aug 08 Javascript
微信小程序HTTP接口请求封装代码实例
Sep 05 Javascript
JavaScript定时器使用方法详解
Mar 26 Javascript
当某个文本框成为焦点时即清除文本框内容
Apr 28 #Javascript
JS实现两个大数(整数)相乘
Apr 28 #Javascript
关于img的href和src取变量及赋值的方法
Apr 28 #Javascript
php和js对数据库图片进行等比缩放示例
Apr 28 #Javascript
JS计算网页停留时间代码
Apr 28 #Javascript
js分页代码分享
Apr 28 #Javascript
jquery使用ajax实现微信自动回复插件
Apr 28 #Javascript
You might like
DC《神奇女侠2》因疫情推迟上映 温子仁新恐怖片《恶性》撤档
2020/04/09 欧美动漫
Laravel 5.3 学习笔记之 配置
2016/08/28 PHP
json跟xml的对比分析
2008/06/10 Javascript
JQuery,Extjs,YUI,Prototype,Dojo 等JS框架的区别和应用场景简述
2010/04/15 Javascript
js 判断checkbox是否选中的操作方法
2012/11/09 Javascript
原生javascript模仿win8等待提示圆圈进度条
2014/04/24 Javascript
nodejs开发环境配置与使用
2014/11/17 NodeJs
Bootstrap表单布局
2016/07/19 Javascript
webpack构建react多页面应用详解
2017/09/15 Javascript
基于dataset的使用和图片延时加载的实现方法
2017/12/11 Javascript
layui实现显示数据表格、搜索和修改功能示例
2020/06/03 Javascript
解决antd Form 表单校验方法无响应的问题
2020/10/27 Javascript
[07:47]DOTA2国际邀请赛采访专栏:探访Valve总部
2013/08/08 DOTA
Python学习之asyncore模块用法实例教程
2014/09/29 Python
Python的Django框架安装全攻略
2015/07/15 Python
python交互式图形编程实例(一)
2017/11/17 Python
对Python 检查文件名是否规范的实例详解
2019/06/10 Python
利用python实现冒泡排序算法实例代码
2019/12/01 Python
python GUI库图形界面开发之PyQt5浏览器控件QWebEngineView详细使用方法
2020/02/26 Python
pytorch 中的重要模块化接口nn.Module的使用
2020/04/02 Python
python使用建议与技巧分享(二)
2020/08/17 Python
Python使用Selenium模拟浏览器自动操作功能
2020/09/08 Python
css和css3弹性盒模型实现元素宽度(高度)自适应
2019/05/15 HTML / CSS
Sofmap官网:日本著名的数码电器专卖店
2017/05/19 全球购物
美国五金商店:Ace Hardware
2018/03/27 全球购物
美国电子产品购物网站:BuyDig.com
2020/06/17 全球购物
成品仓管员岗位职责
2013/12/11 职场文书
运动会通讯稿300字
2014/02/02 职场文书
保安队长职务说明书
2014/02/23 职场文书
学前班评语大全
2014/05/04 职场文书
法学院毕业生求职信
2014/06/25 职场文书
电子银行业务授权委托书
2014/10/10 职场文书
污水处理保证书
2015/05/09 职场文书
靠谱的活动总结
2019/04/16 职场文书
2019 入党申请书范文
2019/07/10 职场文书
数据设计之权限的实现
2022/08/05 MySQL