博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript 简单实现排序算法
阅读量:6871 次
发布时间:2019-06-26

本文共 3233 字,大约阅读时间需要 10 分钟。

/* * 1.插入排序(直接插入排序) * 思想:将一个记录插入到一个已排好序的有序表中,从而得到一个新的、记录增1的有序表。默认将第一个元素看为有序表,然后依次插入后边的元素 * 注意:这里插入元素的时候默认的策略是从后向前看,找第一个比自己小的;而不是从前向后看,找第一个比自己大的, * 时间复杂度:O(n^2)         空间复杂度:O(1)         稳定性:稳定 * */Array.prototype.sortGuo1 = function() {  for (var i = 1; i < this.length; i++) {    var tem = this[i];    for (var j = i - 1; j >= 0 && tem < this[j]; j--) {      this[j + 1] = this[j]; //循环后移比自己tem大的数据    }    this[j + 1] = tem; //找到第一个比自己tem小的位置j,将值插入到该位置后面  }  return this;};var arr1 = [311, 32, 32, 3, 346, 457, 56, 7, 56, 8, 679, 78];alert(arr1.sortGuo1());/* * 2.插入排序(希尔排序)(缩小增量排序) * 思想:希尔排序也是插入排序的一种,是直接针对插入排序进行改进的,该方法又称为"缩小增量排序"。 * 时间复杂度:O(n^2)      空间复杂度:O(1)      稳定性:不稳定 * */Array.prototype.sortGuo2 = function() {  var step = Math.floor(this.length / 2); //初始增量  while (step >= 1) {    for (var i = step; i < this.length; i++) {      var tem = this[i];      for (var j = i - step; j >= 0 && tem < this[j]; j--) {        this[j + step] = this[j]; //循环后移比自己tem大的数据      }      this[j + step] = tem; //找到第一个比自己tem小的位置j,将值插入到该位置后面    }    step = Math.floor(step / 2); //缩小增量step  }  return this;};var arr2 = [311, 32, 32, 3, 346, 457, 56, 7, 56, 8, 679, 78];alert(arr2.sortGuo2());/* * 3.选择排序 * 思想:每一趟排序将会选择出最小的值放在前面 * 时间复杂度:O(n^2)         空间复杂度:O(1)         稳定性:不稳定 * */Array.prototype.sortGuo3 = function() {  for (var i = 0; i < this.length - 1; i++) {    for (var j = i + 1; j < this.length; j++) {      if (this[j] < this[i]) {        var tem = this[i];        this[i] = this[j];        this[j] = tem;      }    }  }  return this;};var arr3 = [311, 32, 32, 3, 346, 457, 56, 7, 56, 8, 679, 78];alert(arr3.sortGuo3());/* * 4.交换排序(冒泡排序) * 思想:重复走访过要排序的序列,从后往前,一次比较两个元素,如果他们的顺序错误就将他们进行交换,每一次冒上来的最小的到剩余待排序序列的顶部。 * 时间复杂度:O(n^2)         空间复杂度:O(1)         稳定性:稳定 * */Array.prototype.sortGuo4 = function() {  for (var i = 0; i < this.length - 1; i++) //n-1趟冒泡,每趟冒出最小的  {    for (var j = this.length - 1; j > i; j--) //n-1 n-2 ...1 比较次数依次递减    {      if (this[j] < this[j - 1]) {        var tem = this[j - 1];        this[j - 1] = this[j];        this[j] = tem;      }    }  }  return this;};var arr4 = [311, 32, 32, 3, 346, 457, 56, 7, 56, 8, 679, 78];alert(arr4.sortGuo4());/* * 5.交换排序(快速排序) * 思想:通过一趟排序将待排记录分割成两个部分,其中一部分记录关键字均比另一部分记录的关键字小,则可以分别对这两部分关键字继续排序,以达到整个序列有序的目的。 * 时间复杂度:O(nlogn),最坏的情况下为O(n^2)         空间复杂度:O(1)         稳定性:不稳定 * */function sortGuo5(arr, leftIndex, rightIndex) {  if (leftIndex < rightIndex) {    var index = quickSortGuo(arr, leftIndex, rightIndex); //进行一趟排序    sortGuo5(arr, leftIndex, index);    sortGuo5(arr, index + 1, rightIndex);  }  return arr;}function quickSortGuo(arr, leftIndex, rightIndex) {  var base = arr[leftIndex];  var tem;  while (leftIndex < rightIndex) {    while (leftIndex < rightIndex && arr[rightIndex] >= base) {      rightIndex--;    }    tem = arr[rightIndex];    arr[rightIndex] = arr[leftIndex];    arr[leftIndex] = tem;    while (leftIndex < rightIndex && arr[leftIndex] <= base) {      leftIndex++;    }    tem = arr[rightIndex];    arr[rightIndex] = arr[leftIndex];    arr[leftIndex] = tem;  }  return leftIndex;}var arr5 = [311, 32, 32, 3, 346, 457, 56, 7, 56, 8, 679, 78];alert(sortGuo5(arr5, 0, arr5.length - 1));

 

转载于:https://www.cnblogs.com/fengyouqi/p/7766171.html

你可能感兴趣的文章
又见尾递归
查看>>
安装PyGraphics
查看>>
【COCOS2DX-LUA 脚本开发之四】使用TOLUA++编译PKG,从而创建自定义类让LUA脚本使用...
查看>>
开源大数据周刊-第16期
查看>>
遥感图像分类现状及存在的问题
查看>>
Commons Logging存在的ClassLoader问题详解
查看>>
双向链表的操作
查看>>
Flume-ng 高级功能配置
查看>>
我的友情链接
查看>>
CRM技术发展历程
查看>>
编译安装LAMP(php-fpm)步骤详解
查看>>
2-Ceph运维
查看>>
深入浅出Linux设备驱动编程--定时器
查看>>
常见移动设备的 CSS3 Media Query 整理(iPhone/iPad/Galaxy/HTC
查看>>
java递归-迷宫求解
查看>>
springboot加载顺序
查看>>
python chapter 学习之序列
查看>>
GlusterFS的基础应用
查看>>
ORA-09925: Unable to create audit trail file Linux-x86_64
查看>>
如何跳出嵌套语句之return
查看>>