常见排序方法_js

function ArrayList() {
        this.array = [];
        ArrayList.prototype.insert = function (item) {
          this.array.push(item);
        };
        ArrayList.prototype.toString = function () {
          return this.array.join("-");
        };
        ArrayList.prototype.swap = function (a, b) {
          // temp=this.array[a]
          // this.array[a]=this.array[b]
          // this.array[b]=temp
          // 交换数组中的两个元素
          [this.array[a], this.array[b]] = [this.array[b], this.array[a]];
        };

        // 冒泡排序
        ArrayList.prototype.bubbleSort = function () {
          for (let i = 0; i < this.array.length - 1; i++) {
            for (let j = 0; j < this.array.length - 1 - i; j++) {
              if (this.array[j] > this.array[j + 1]) {
                this.swap(j, j + 1);
              }
            }
          }
          return this.array;
        };

        // 选择排序
        ArrayList.prototype.selectionSort = function () {
          for (let j = 0; j < this.array.length - 1; j++) {
            var min = j;
            for (let i = min + 1; i < this.array.length; i++) {
              if (this.array[min] > this.array[i]) {
                min = i;
              }
            }
            this.swap(j, min);
          }

          return this.array;
        };

        // 插入排序
        ArrayList.prototype.insertSort = function () {
          for (var i = 1; i < this.array.length; i++) {
            // for(var j=i-1;j>=0;j--){
            //     if(this.array[i]<this.array[j]){
            //         this.array.splice(j,0,this.array[i])
            //     }
            // }
            var temp = this.array[i];
            var j = i;
            while (this.array[j - 1] > temp && j > 0) {
              this.array[j] = this.array[j - 1];
              j--;
            }
            this.array[j] = temp;
          }
          return this.array;
        };

        // 希尔排序
        ArrayList.prototype.shellSort = function () {
          // 初始化增量gap
          var gap = Math.floor(this.array.length / 2);
          while (gap >= 1) {
            for (var i = gap; i < this.array.length; i++) {
              var temp = this.array[i];
              var j = i;
              while (this.array[j - gap] > temp && j > gap - 1) {
                this.array[j] = this.array[j - gap];
                j -= gap;
              }
              this.array[j] = temp;
            }
            gap = Math.floor(gap / 2);
          }
          return this.array;
        };

        // 快速排序
        // 选择枢纽
        ArrayList.prototype.median = function (left, right) {
          var center = Math.floor((left + right) / 2);
          if (this.array[left] > this.array[center]) {
            this.swap(left, center);
          }

          if (this.array[center] > this.array[right]) {
            this.swap(center, right);
          }
          if (this.array[left] > this.array[center]) {
            this.swap(left, center);
          }
          // 将center换到right-1
          this.swap(center, right - 1);
          return this.array[right - 1];
        };
        ArrayList.prototype.quickSort = function () {
          this.quick(0, this.array.length - 1);
        };
        ArrayList.prototype.quick = function (left, right) {
          if (left >= right) return;
          // 获取枢纽
          var pivot = this.median(left, right);
          var i = left;
          var j = right - 1;
          while (true) {
            while (this.array[++i] < pivot) {}
            while (this.array[--j] > pivot) {}
            if (i < j) {
              this.swap(i, j);
            } else {
              break;
            }
          }
          this.swap(i, right - 1);
          this.quick(left, i - 1);
          this.quick(i + 1, right);
        };

        ArrayList.prototype.findMax = function () {
          var max = 0;
          for (var i = 0; i < this.array.length; i++) {
            if (this.array[i] > max) {
              max = this.array[i];
            }
            return max;
          }
        };
        ArrayList.prototype.countingSort = function () {
          if (this.array.length < 2) {
            return array;
          }
          const maxValue = this.findMax();
          let sortedIndex = 0;
          const counts = new Array(maxValue + 1);
          this.array.forEach((element) => {
            if (!counts[element]) {
              counts[element] = 0;
            }
            counts[element]++;
          });
          counts.forEach((element, i) => {
            console.log(element);
            while (element > 0) {
              this.array[sortedIndex++] = i;
              element--;
            }
          });
          return this.array;
        };


      }

      // test
      var list = new ArrayList();
      list.insert(66);
      list.insert(88);
      list.insert(12);
      list.insert(87);
      list.insert(100);
      list.insert(5);
      list.insert(566);
      list.insert(23);
      console.log(list.toString());
      // console.log(list.bubbleSort())
      // console.log('selection',list.selectionSort());
      // console.log('insert',list.insertSort());
      // console.log('shell',list.shellSort());
    //   list.quickSort();
    //   console.log("quick", list);
    list.countingSort()
    console.log('countingSort',list);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值