zoukankan      html  css  js  c++  java
  • js冒泡排序,快速排序,插入排序

     //冒泡排序
    function sortBubble(array){
      var len=array.length,i,j,tmp;
      for(i=len-1;i>=1;i--){
        for(j=0;j<=i-1;j++){
          if(array[j]>array[j+1]){
             d=array[j+1];
             array[j+1]=array[j];
             array[j]=d;
          }
        }
      }
      return array;
    }
    //快速排序
    function sortQuick(array){
      var low=0,high=array.length-1;
      var sort=function(low,high){
        if(low==high){
           return;
        }
        var key=array[low];
        var tmplow=low;
        var tmphigh=high;
        while(low<high){
          while(low<high&&key<=array[high]){
            --high;
          }
          array[low]=array[high];
          while(low<high&&array[low]<=key)
          {
             ++low;
          }
          array[high]=array[low];
          if(low==tmplow){
            sort(++low,tmphigh);
            return;
          }
        };
        array[low]=key;
        sort(tmplow,low-1);
        sort(high+1,tmphigh);
      };
      sort(low,high);
      sort();
      return array;
    }
    //插入排序
    function sortInsert(array){
      var i=1,j,len=array.length,key;
      for(;i<len;i++){
        j=i;
        key=array[j];
        while(--j>-1){
          if(array[j]>key){
            array[j+1]=array[j];
          }
          else
          {
             break;
          }
        }
        array[j+1]=key;
      }
      return array;
    }
  • 相关阅读:
    [NOI2010] 能量采集 (数学)
    mysql双主操作记录
    linux查看版本
    netty
    idea修改文件,target目录对应的文件消失
    oracle11g的分区(range、list)索引测试
    There is a cycle in the hierarchy解决办法
    git学习转廖雪峰
    解决SVN Cleanup错误: Failed to run the WC DB work queue associated with
    nginx之 proxy_pass
  • 原文地址:https://www.cnblogs.com/liujinyu/p/3555242.html
Copyright © 2011-2022 走看看