Ruby's Louvre

每天学习一点点算法

导航

组合排序

        var combSort = function(array){
          var gap = array.length;
          do{
            gap = gap * 10 / 13
            if(gap === 9 || gap === 10)
              gap = 11
            if(gap < 1){
              gap = 1
            }
            var swapped = false;
            for(var i=0;i<array.length-gap;i++){
              var j = i + gap
              if(array[i]>array[j]){
                var temp = array[i];
                array[i] = array[j];
                array[j] = temp;
                test(array)
                swapped = true
              }
            }
            if(gap == 1 && !swapped){
              break;
            }
          }while(1);
        }

posted on 2010-01-20 18:34  司徒正美  阅读(1051)  评论(0编辑  收藏  举报