javascript排序算法总结

javascript排序算法总结

这里是对数据结构中的几种算法做一个总结
简单的排序算法有:①冒泡排序 ②选择排序 ③插入排序
高级的排序算法有: ①希尔排序 ②快速排序

一、冒泡排序

如图所示为冒泡排序的步骤
在这里插入图片描述
这里冒泡排序的查找的时间复杂度是O(n ^ 2),交换的时间复杂度是O(n^2)。

二、选择排序

如图所示为选择排序的一般步骤
在这里插入图片描述
其思路就是找到剩下的元素中最小的一个,然后进行交换,其时间复杂度是O(n ^ 2),比冒泡排序效率高一些

三、插入排序

如图所示下面是插入排序的一般步骤
在这里插入图片描述
其基本思路就是:将后面为排序的元素,取出依次比较然后插入前面已经排好序的元素。
其时间复杂度是O(n ^2),但是相对于前面的算法,比较来说效率更高。

四、希尔排序

如图所示下面是插入排序的一般步骤
在这里插入图片描述
希尔排序实现思路:首先进行分组,然后将分组的进行插入排序,随后设置组的跨度逐渐减小,当组的跨度为1的时候,就是插入排序。
增量设置:
一、希尔排序原版中增量是N/2
二、Hibbard增量为2k-1,选择的是奇数,当有100个数的时候,选择增量就是51,这种增量最坏的时间复杂度是O(N^3/2),猜想的平均复杂度是O(N5/4)
三、Sedgewick增量
{1,5,19,41,109…},这种增量的最坏的复杂度是O(N^4/3),平均复杂度是O(N ^ 7/6)

五、快速排序

如图所示
在这里插入图片描述
思路设计:这里使用到递归处理。这里为了尽可能的让选择的元素处于中间,首先应该将left和right以及中间元素进行比较,三个位置进行从小到大的顺序排列如图所示
在这里插入图片描述
全部代码实现如下:

<script>
function ArrayList() {
  // 属性
  this.arrayList = [];
  // 方法
  // 1、插入方法
  ArrayList.prototype.insert = function (item) {
    this.arrayList.push(item);
  }
  // 2、实现toString
  ArrayList.prototype.toString = function () {
    return this.arrayList.join("——");
  }
  // 3、实现两者交换
  ArrayList.prototype.swap = function (m, n) {
    let temp = this.arrayList[m];
    this.arrayList[m] = this.arrayList[n];
    this.arrayList[n] = temp;
  }
  // 4、实现冒泡排序
  ArrayList.prototype.bubbleSort = function () {
    let length = this.arrayList.length;
    for (let i = length - 1; i >= 0; i--) {
      for (let j = 0; j < i; j++) {
        if (this.arrayList[j] > this.arrayList[j + 1]) {
          this.swap(j, j + 1)
        }
      }
    }
  }
  // 5、实现选择排序
  ArrayList.prototype.selectionSort = function () {
    let length = this.arrayList.length;
    for (let j = 0; j < length - 1; j++) {
      let min = j;
      for (let i = min + 1; i < length; i++) {
        if (this.arrayList[min] > this.arrayList[i]) {
          min = i;
        }
      }
      this.swap(min, j);
    }
  }
  // 6、实现插入排序
  ArrayList.prototype.insertSort = function () {
    let length = this.arrayList.length;
    for (let i = 1; i < length; i++) {
      let temp = this.arrayList[i];
      let j = i;
      while (temp < this.arrayList[j - 1] && j >= 0) {
        this.arrayList[j] = this.arrayList[j - 1];
        j--;
      }
      this.arrayList[j] = temp;
    }
  }
  // 7、实现希尔排序
  ArrayList.prototype.shellSort = function () {
    let length = this.arrayList.length;
    let gap = Math.floor(length / 2);
    while (gap >= 1) {
      for (let i = gap; i < length; i++) {
        let temp = this.arrayList[i];
        let j = i;
        while (temp < this.arrayList[j - gap] && j > gap - 1) {
          this.arrayList[j] = this.arrayList[j - gap];
          j -= gap;
        }
        this.arrayList[j] = temp;
      }
      gap = Math.floor(gap / 2);
    }
  }
  // 8、实现快速排序枢纽的代码
  ArrayList.prototype.median = function (left, right) {
    let medi = Math.floor((left + right) / 2);
    if (this.arrayList[left] > this.arrayList[medi]) {
      this.swap(left, medi);
    }
    if (this.arrayList[left] > this.arrayList[right]) {
      this.swap(left, right);
    }
    if (this.arrayList[medi] > this.arrayList[right]) {
      this.swap(medi, right);
    }
    this.swap(medi, right - 1);
    return this.arrayList[right - 1];
  }
  // 9、实现快速排序代码
  ArrayList.prototype.quickSort = function () {
    let length = this.arrayList.length;
    this.quick(0, length - 1);
  }
  // 快速排序算法递归实现
  ArrayList.prototype.quick = function (left, right) {
    if (left >= right) return;
    let mid = this.median(left, right);
    let i = left;
    let j = right - 1;
    while (i < j) {
      while (this.arrayList[++i] < mid) {}
      while (this.arrayList[--j] > mid) {}
      if (i < j) {
        this.swap(i, j);
      } else {
        break;
      }
    }
    this.swap(i, right - 1);
    this.quick(left, i - 1);
    this.quick(i + 1, right);
  }
}
</script>

希尔排序和快速排序的时间复杂度都要小于O(n^2)。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值