几种经典排序算法的JS实现方法


Posted in Javascript onMarch 25, 2016

一.冒泡排序

function BubbleSort(array) {
  var length = array.length;
  for (var i = length - 1; i > 0; i--) { //用于缩小范围
    for (var j = 0; j < i; j++) { //在范围内进行冒泡,在此范围内最大的一个将冒到最后面
      if (array[j] > array[j+1]) { 
        var temp = array[j];
        array[j] = array[j+1];
        array[j+1] = temp;
      }
    }
    console.log(array);
    console.log("-----------------------------");
  }
  return array;
}


var arr = [10,9,8,7,7,6,5,11,3];
var result = BubbleSort(arr);
console.log(result); 
/*
[ 9, 8, 7, 7, 6, 5, 10, 3, 11 ]
-----------------------------
[ 8, 7, 7, 6, 5, 9, 3, 10, 11 ]
-----------------------------
[ 7, 7, 6, 5, 8, 3, 9, 10, 11 ]
-----------------------------
[ 7, 6, 5, 7, 3, 8, 9, 10, 11 ]
-----------------------------
[ 6, 5, 7, 3, 7, 8, 9, 10, 11 ]
-----------------------------
[ 5, 6, 3, 7, 7, 8, 9, 10, 11 ]
-----------------------------
[ 5, 3, 6, 7, 7, 8, 9, 10, 11 ]
-----------------------------
[ 3, 5, 6, 7, 7, 8, 9, 10, 11 ]
-----------------------------
[ 3, 5, 6, 7, 7, 8, 9, 10, 11 ]
*/

二.选择排序

function SelectionSort(array) {
  var length = array.length;
  for (var i = 0; i < length; i++) { //缩小选择的范围
    var min = array[i]; //假定范围内第一个为最小值
    var index = i; //记录最小值的下标
    for (var j = i + 1; j < length; j++) { //在范围内选取最小值
      if (array[j] < min) {
        min = array[j];
        index = j;
      }
    }
    if (index != i) { //把范围内最小值交换到范围内第一个
      var temp = array[i];
      array[i] = array[index];
      array[index] = temp;
    }
    console.log(array);
    console.log("---------------------");
  }
  return array;
}

var arr = [ 1, 10, 100, 90, 65, 5, 4, 10, 2, 4 ];
var result = SelectionSort(arr);
console.log(result);
/*
[ 1, 10, 100, 90, 65, 5, 4, 10, 2, 4 ]
---------------------
[ 1, 2, 100, 90, 65, 5, 4, 10, 10, 4 ]
---------------------
[ 1, 2, 4, 90, 65, 5, 100, 10, 10, 4 ]
---------------------
[ 1, 2, 4, 4, 65, 5, 100, 10, 10, 90 ]
---------------------
[ 1, 2, 4, 4, 5, 65, 100, 10, 10, 90 ]
---------------------
[ 1, 2, 4, 4, 5, 10, 100, 65, 10, 90 ]
---------------------
[ 1, 2, 4, 4, 5, 10, 10, 65, 100, 90 ]
---------------------
[ 1, 2, 4, 4, 5, 10, 10, 65, 100, 90 ]
---------------------
[ 1, 2, 4, 4, 5, 10, 10, 65, 90, 100 ]
---------------------
[ 1, 2, 4, 4, 5, 10, 10, 65, 90, 100 ]
---------------------
[ 1, 2, 4, 4, 5, 10, 10, 65, 90, 100 ]
*/

三.插入排序

function InsertionSort(array) {
  var length = array.length;
  for (var i = 0; i < length - 1; i++) {
    //i代表已经排序好的序列最后一项下标
    var insert = array[i+1];
    var index = i + 1;//记录要被插入的下标
    for (var j = i; j >= 0; j--) {
      if (insert < array[j]) {
        //要插入的项比它小,往后移动
        array[j+1] = array[j];
        index = j;
      }
    }
    array[index] = insert;
    console.log(array);
    console.log("-----------------------");
  }
  return array;
}

var arr = [100,90,80,62,80,8,1,2,39];
var result = InsertionSort(arr);
console.log(result);
/*
[ 90, 100, 80, 62, 80, 8, 1, 2, 39 ]
-----------------------
[ 80, 90, 100, 62, 80, 8, 1, 2, 39 ]
-----------------------
[ 62, 80, 90, 100, 80, 8, 1, 2, 39 ]
-----------------------
[ 62, 80, 80, 90, 100, 8, 1, 2, 39 ]
-----------------------
[ 8, 62, 80, 80, 90, 100, 1, 2, 39 ]
-----------------------
[ 1, 8, 62, 80, 80, 90, 100, 2, 39 ]
-----------------------
[ 1, 2, 8, 62, 80, 80, 90, 100, 39 ]
-----------------------
[ 1, 2, 8, 39, 62, 80, 80, 90, 100 ]
-----------------------
[ 1, 2, 8, 39, 62, 80, 80, 90, 100 ]
*/

四.希尔排序

function ShellSort(array) {
  var length = array.length;
  var gap = Math.round(length / 2);
  while (gap > 0) {
    for (var i = gap; i < length; i++) {
      var insert = array[i];
      var index = i;
      for (var j = i; j >= 0; j-=gap) {
        if (insert < array[j]) {
          array[j+gap] = array[j];
          index = j;
        }
      }
      array[index] = insert;
    }
    console.log(array);
    console.log("-----------------------");
    gap = Math.round(gap/2 - 0.1);
  }
  return array;
}

var arr = [ 13, 14, 94, 33, 82, 25, 59, 94, 65, 23, 45, 27, 73, 25, 39, 10 ];
var result = ShellSort(arr);
console.log(result); 
/*
[ 13, 14, 45, 27, 73, 25, 39, 10, 65, 23, 94, 33, 82, 25, 59, 94 ]
-----------------------
[ 13, 14, 39, 10, 65, 23, 45, 27, 73, 25, 59, 33, 82, 25, 94, 94 ]
-----------------------
[ 13, 10, 39, 14, 45, 23, 59, 25, 65, 25, 73, 27, 82, 33, 94, 94 ]
-----------------------
[ 10, 13, 14, 23, 25, 25, 27, 33, 39, 45, 59, 65, 73, 82, 94, 94 ]
-----------------------
[ 10, 13, 14, 23, 25, 25, 27, 33, 39, 45, 59, 65, 73, 82, 94, 94 ]
*/

五.归并排序

function MergeSort(array) {
  var length = array.length;
  if (length <= 1) {
    return array;
  } else {
    var num = Math.ceil(length/2);
    var left = MergeSort(array.slice(0, num));
    var right = MergeSort(array.slice(num, length));
    return merge(left, right);
  }
}

function merge(left, right) {
  console.log(left);
  console.log(right);
  var a = new Array();
  while (left.length > 0 && right.length > 0) {
    if (left[0] <= right[0]) {
      var temp = left.shift();
      a.push(temp);
    } else {
      var temp = right.shift();
      a.push(temp);
    }
  }
  if (left.length > 0) {
    a = a.concat(left);
  }
  if (right.length > 0) {
    a = a.concat(right);
  }
  console.log(a);
  console.log("-----------------------------");
  return a;
}

var arr = [ 13, 14, 94, 33, 82, 25, 59, 94, 65, 23, 45, 27, 73, 25, 39, 10 ];
var result = MergeSort(arr);
console.log(result);
/*
[ 13 ]
[ 14 ]
[ 13, 14 ]
-----------------------------
[ 94 ]
[ 33 ]
[ 33, 94 ]
-----------------------------
[ 13, 14 ]
[ 33, 94 ]
[ 13, 14, 33, 94 ]
-----------------------------
[ 82 ]
[ 25 ]
[ 25, 82 ]
-----------------------------
[ 59 ]
[ 94 ]
[ 59, 94 ]
-----------------------------
[ 25, 82 ]
[ 59, 94 ]
[ 25, 59, 82, 94 ]
-----------------------------
[ 13, 14, 33, 94 ]
[ 25, 59, 82, 94 ]
[ 13, 14, 25, 33, 59, 82, 94, 94 ]
-----------------------------
[ 65 ]
[ 23 ]
[ 23, 65 ]
-----------------------------
[ 45 ]
[ 27 ]
[ 27, 45 ]
-----------------------------
[ 23, 65 ]
[ 27, 45 ]
[ 23, 27, 45, 65 ]
-----------------------------
[ 73 ]
[ 25 ]
[ 25, 73 ]
-----------------------------
[ 39 ]
[ 10 ]
[ 10, 39 ]
-----------------------------
[ 25, 73 ]
[ 10, 39 ]
[ 10, 25, 39, 73 ]
-----------------------------
[ 23, 27, 45, 65 ]
[ 10, 25, 39, 73 ]
[ 10, 23, 25, 27, 39, 45, 65, 73 ]
-----------------------------
[ 13, 14, 25, 33, 59, 82, 94, 94 ]
[ 10, 23, 25, 27, 39, 45, 65, 73 ]
[ 10, 13, 14, 23, 25, 25, 27, 33, 39, 45, 59, 65, 73, 82, 94, 94 ]
-----------------------------
[ 10, 13, 14, 23, 25, 25, 27, 33, 39, 45, 59, 65, 73, 82, 94, 94 ]
*/

六.快速排序

function QuickSort(array) {
  var length = array.length;
  if (length <= 1) {
    return array;
  } else {
    var smaller = [];
    var bigger = [];
    var base = [array[0]];
    for (var i = 1; i < length; i++) {
      if (array[i] <= base[0]) {
        smaller.push(array[i]);
      } else {
        bigger.push(array[i]);
      }
    }
    console.log(smaller.concat(base.concat(bigger)));
    console.log("-----------------------");
    return QuickSort(smaller).concat(base.concat(QuickSort(bigger)));
  }
}


var arr = [ 8, 10, 100, 90, 65, 5, 4, 10, 2, 4 ];
var result = QuickSort(arr);
console.log(result);
/*
[ 5, 4, 2, 4, 8, 10, 100, 90, 65, 10 ]
-----------------------
[ 4, 2, 4, 5 ]
-----------------------
[ 2, 4, 4 ]
-----------------------
[ 2, 4 ]
-----------------------
[ 10, 10, 100, 90, 65 ]
-----------------------
[ 90, 65, 100 ]
-----------------------
[ 65, 90 ]
-----------------------
[ 2, 4, 4, 5, 8, 10, 10, 65, 90, 100 ]
*/

以上这篇几种经典排序算法的JS实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
jQuery实现返回顶部效果的方法
May 29 Javascript
详解jQuery Mobile自定义标签
Jan 06 Javascript
JavaScript实现复制或剪切内容到剪贴板功能的方法
May 23 Javascript
简洁实用的BootStrap jQuery手风琴插件
Aug 31 Javascript
利用jquery给指定的table动态添加一行、删除一行的方法
Oct 12 Javascript
jQuery插件FusionCharts绘制的3D饼状图效果实例【附demo源码下载】
Mar 03 Javascript
JavaScript实现图片切换效果
Aug 12 Javascript
简单学习5种处理Vue.js异常的方法
Jun 17 Javascript
微信小程序实现点击空白隐藏的方法示例
Aug 13 Javascript
js+audio实现音乐播放器
Sep 13 Javascript
js实现鼠标切换图片(无定时器)
Jan 27 Javascript
原生JS实现飞机大战小游戏
Jun 09 Javascript
javascript拖拽应用实例
Mar 25 #Javascript
JavaScript学习笔记之创建对象
Mar 25 #Javascript
JS实现的颜色实时渐变效果完整实例
Mar 25 #Javascript
JavaScript学习笔记之ES6数组方法
Mar 25 #Javascript
JS实现点击登录弹出窗口同时背景色渐变动画效果
Mar 25 #Javascript
JS实现响应鼠标点击动画渐变弹出层效果代码
Mar 25 #Javascript
JS+CSS实现鼠标经过弹出一个DIV框完整实例(带缓冲动画渐变效果)
Mar 25 #Javascript
You might like
实现PHP+Mysql无限分类的方法汇总
2015/03/02 PHP
Thinkphp关闭缓存的方法
2015/06/26 PHP
javascript 异常处理使用总结
2009/06/21 Javascript
Prototype Hash对象 学习
2009/07/19 Javascript
Javascript string 扩展库代码
2010/04/09 Javascript
EXTJS FORM HIDDEN TEXTFIELD 赋值 使用value不好用的问题
2011/04/16 Javascript
基于jquery的网站幻灯片切换效果焦点图代码
2013/09/15 Javascript
node.js实现BigPipe详解
2014/12/05 Javascript
JavaScript中的类(Class)详细介绍
2014/12/30 Javascript
jQuery层动画定位滑动效果的方法
2015/04/30 Javascript
原生js和jquery分别实现横向导航菜单效果
2016/05/13 Javascript
原生js实现淘宝购物车功能
2020/06/23 Javascript
axios基本入门用法教程
2017/03/25 Javascript
bootstrap table使用入门基本用法
2017/05/24 Javascript
微信小程序使用gitee进行版本管理
2018/09/20 Javascript
jquery使用FormData实现异步上传文件
2018/10/25 jQuery
浅谈Vue.js中如何实现自定义下拉菜单指令
2019/01/06 Javascript
使用layui监听器监听select下拉框,事件绑定不成功的解决方法
2019/09/28 Javascript
javascript实现前端input密码输入强度验证
2020/06/24 Javascript
vue 需求 data中的数据之间的调用操作
2020/08/05 Javascript
原生js拖拽功能制作滑动条实例代码
2021/02/05 Javascript
JS canvas实现画板和签字板功能
2021/02/23 Javascript
python实现对一个完整url进行分割的方法
2015/04/29 Python
Python自定义函数实现求两个数最大公约数、最小公倍数示例
2018/05/21 Python
Python使用jsonpath-rw模块处理Json对象操作示例
2018/07/31 Python
解析Python的缩进规则的使用
2019/01/16 Python
在Pycharm中修改文件默认打开方式的方法
2019/01/17 Python
python实现dijkstra最短路由算法
2019/01/17 Python
对YOLOv3模型调用时候的python接口详解
2019/08/26 Python
python实现替换word中的关键文字(使用通配符)
2020/02/13 Python
python用TensorFlow做图像识别的实现
2020/04/21 Python
keras实现VGG16 CIFAR10数据集方式
2020/07/07 Python
超越自我演讲稿
2014/05/21 职场文书
结对共建工作方案
2014/06/02 职场文书
《刺客之王:C罗全景传记》:时代从来不会亏待手艺人
2019/11/28 职场文书
java获取一个文本文件的编码(格式)信息
2022/09/23 Java/Android