DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> javascript數組排序匯總
javascript數組排序匯總
編輯:關於JavaScript     

javascript數組排序匯總

//排序算法
  window.onload = function(){
    var array = [0,1,2,44,4,
          324,5,65,6,6,
          34,4,5,6,2,
          43,5,6,62,43,
          5,1,4,51,56,
          76,7,7,2,1,
          45,4,6,7,8];
    //var array = [4,2,5,1,0,3];
    console.log('原始數組');
    console.log(array);
    array = sorting.shellSort(array);
    // alert(array);
    console.log('排序後的數組')
    console.log(array);
  }
   
  var sorting = {
    //利用sort方法進行排序
    systemSort: function(arr){
      return arr.sort(function(a,b){
        return a-b;
      });
    },
   
    //冒泡排序
    bubbleSort: function(arr){
      var len=arr.length, tmp;
      for(var i=0;i<len-1;i++){
        for(var j=0;j<len-1-i;j++){
          if(arr[j]>arr[j+1]){
            tmp = arr[j];
            arr[j] = arr[j+1];
            arr[j+1] = tmp;
          }
        }
      }
      return arr;
    },
   
    //快速排序
    quickSort: function(arr){
      var low=0, high=arr.length-1;
      sort(low,high);
      function sort(low, high){
        if(low<high){
          var mid = (function(low, high){
            var tmp = arr[low];
            while(low<high){
              while(low<high&&arr[high]>=tmp){
                high--;
              }
              arr[low] = arr[high];
              while(low<high&&arr[low]<=tmp){
                low++;
              }
              arr[high] = arr[low];
            }
            arr[low] = tmp;
            return low;
          })(low, high);
          sort(low, mid-1);
          sort(mid+1,high);
        }
      }
      return arr;
    },
   
    //插入排序
    insertSort: function(arr){
      var len = arr.length;
      for(var i=1;i<len;i++){
        var tmp = arr[i];
        for(var j=i-1;j>=0;j--){
          if(tmp<arr[j]){
            arr[j+1] = arr[j];
          }else{
            arr[j+1] = tmp;
            break;
          }
        }
      }
      return arr;
    },
   
    //希爾排序
    shellSort: function(arr){
      var h = 1;
      while(h<=arr.length/3){
        h = h*3+1; //O(n^(3/2))by Knuth,1973
      }
      for( ;h>=1;h=Math.floor(h/3)){
        for(var k=0;k<h;k++){
          for(var i=h+k;i<arr.length;i+=h){
            for(var j=i;j>=h&&arr[j]<arr[j-h];j-=h){
              var tmp = arr[j];
              arr[j] = arr[j-h];
              arr[j-h] = tmp;
            }
          }
        }
      }
      return arr;
    }
  }

以上所述就是本文的全部內容了,希望大家能夠喜歡。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved