zoukankan      html  css  js  c++  java
  • 【js基础】js排序方法——快排+堆排+插排+选择排

    快排

            Array.prototype.fastSort = function(){
                     var arr = this;
    
                     function sort(left, right, arr){
                         if( left >= right){
                             return;
                         }
                         var key = arr[left];
                         var i = left;
                         var j = right;
                         
                         while(i < j){
                             while(i < j && arr[j] >= key){
                                 j--;
                             }
                             arr[i] = arr[j];
                             while(i < j && arr[i] <= key){
                                 i++;
                             }
                             arr[j] = arr[i];
                         }
                         arr[i] = key;
                         sort(left, i - 1, arr);
                         sort(i + 1, right, arr);
                     }
    
                     sort(0, arr.length - 1, arr);
    
                     return arr;
                 }

    插排

            Array.prototype.insertSort = function(){
                     var arr = this;
                     var len = arr.length;
                     for(var i = 1; i < len; i ++ ){
                         var j = i - 1;
                         var tmp = arr[i];
                         while(j >= 0 && tmp < arr[j]){
                             arr[j + 1] = arr[j];
                             j--;
                         }
                         if(j != i - 1){
                             arr[j + 1] = tmp;
                         }
                     }
                     return arr;
                 }

    选择排序

            Array.prototype.selectSort = function(){
                     var arr = this;
                     var len = arr.length;
                     var min;
                     var tmp;
                     for(var i = 0; i < len - 1; i ++ ){
                         min = i;
                         for(var j = i + 1; j < len; j++){
                             if(arr[min] > arr[j]){
                                 min = j;
                             }
                         }
                         if(min != i){
                             tmp = arr[min];
                             arr[min] = arr[i];
                             arr[i] = tmp;
                         }
                     }
                     return arr;
                 }

    堆排序

           Array.prototype.swap = function(i, j){
                    var tmp = this[i];
                    this[i] = this[j];
                    this[j] = tmp;
                }
    
                //大顶堆
                Array.prototype.buildMaxHeap = function(){
                    for(var i = Math.floor(this.length/2) - 1; i >= 0; i--){
                        this.heapAdjust(i, this.length);
                    }
                };
    
                //调整堆
                Array.prototype.heapAdjust = function(i, j){
                    var max = i;
                    var left = 2 * i + 1;
                    var right = 2 * i + 2;
                    if(left < j && this[max] < this[left]){
                        max = left;
                    }                 
                    if(right < j && this[max] < this[right]){
                        max = right;
                    }                 
                    if(max != i){
                        this.swap(i,max);
                        this.heapAdjust(max,j);
                    };
                }
                
                //堆排序
                Array.prototype.heapSort = function(){
                    this.buildMaxHeap();
                    for(var i = this.length - 1; i >= 0 ; i--){
                        this.swap(0, i);
                        this.heapAdjust(0, i);
                    }
                    return this;
                };
    只有足够努力,才能看起来毫不费力
  • 相关阅读:
    51nod 1254 最大子段和 V2
    51nod 1115 最大M子段和 V3
    51nod 1053 最大M子段和 V2
    51nod 1052 最大M子段和
    51nod 1051 最大子矩阵和
    web.config或App.config中AttachDBFilenamex相对路径问题
    [转帖]unity3D OnTriggerEnter和OnCollisionEnter的一点个人心得(主要讲区别)
    unity3d 第一人称脚本解释MouseLook
    unity3d-游戏实战突出重围,整合游戏
    unity3d-游戏实战突出重围,第四天 添加角色
  • 原文地址:https://www.cnblogs.com/codelovers/p/4799095.html
Copyright © 2011-2022 走看看