<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8" />
  <title>排序</title>
  <script language="javascript" type="text/javascript">
    function MarkRandomArray() {
      var a = [];
      for (var _i = 0; _i < 10; _i++) {
        a[_i] = parseInt(Math.random() * 100);
      }
      return a;
    }
    //选择排序
    function SelectSort(a) {
      var _swapCnt = 0;
      var _loopCnt = 0;
      for (var _i = 0; _i < a.length; _i++) {
        _loopCnt++;
        var _hasMin = false;
        var _iMin = _i;
        var _loopCnt2 = 0;
        for (var _i2 = _i + 1; _i2 < a.length; _i2++) {
          _loopCnt2++;
          if (a[_iMin] > a[_i2]) {
            _iMin = _i2;;
            _hasMin = true;
          }
        }
        _loopCnt = _loopCnt + (_loopCnt2 > 0 ? _loopCnt2 - 1 : 0);

        if (_hasMin) {
          _swapCnt += 2;
          var _aV = a[_i];
          a[_i] = a[_iMin];
          a[_iMin] = _aV;
          console.log(_i, " : ", a);
        }
      }

      console.log(" _swapCnt= ", _swapCnt);
      console.log(" _loopCnt= ", _loopCnt);
    }


    //冒泡排序
    function PopuSort(a) {
      var _swapCnt = 0;
      var _loopCnt = 0;
      for (var _i = 0; _i < a.length; _i++) {
        _loopCnt++;
        var _iLastSwap = -1;
        var _loopCnt2 = 0;
        for (var _i2 = a.length - 1 ; _i2 > _i; _i2--) {
          _loopCnt2++;
          var _iPrev = _i2 - 1;
          if (a[_i2] < a[_iPrev]) {
            _swapCnt += 2;
            var _aV = a[_iPrev];
            a[_iPrev] = a[_i2];
            a[_i2] = _aV;
            _iLastSwap = _i2;
          }
        }
        _loopCnt = _loopCnt + (_loopCnt2 > 0 ? _loopCnt2 - 1 : 0);
        if (_iLastSwap > 0) {
          console.log(_i, " : ", a);
          _i = _iLastSwap - 1;
        } else {
          break;
        }
      }

      console.log(" _swapCnt= ", _swapCnt);
      console.log(" _loopCnt= ", _loopCnt);
    }

    //插入排序
    function InsertSort(a) {
      var _swapCnt = 0;
      var _loopCnt = 0;
      for (var _i = 0; _i < a.length - 1; _i++) {
        _loopCnt++;
        var _iNext = _i + 1;
        if (a[_i] > a[_iNext]) {
          var _iIns = 0;
          var _loopCnt2 = 0;
          for (var _i2 = _i - 1 ; _i2 >= 0; _i2--) {//包含0元素
            _loopCnt2++;
            if (a[_i2] < a[_iNext]) {
              _iIns = _i2 + 1;
              break;
            }
          }
          _loopCnt = _loopCnt + (_loopCnt2 > 0 ? _loopCnt2 - 1 : 0);


          var _aV = a[_iNext];
          for (var _i3 = _iNext ; _i3 > _iIns; _i3--) {
            _swapCnt++;
            a[_i3] = a[_i3 - 1];
          }
          _swapCnt++;
          a[_iIns] = _aV;
          console.log(_iIns, " : ", a);
        }
      }
      console.log(" _swapCnt= ", _swapCnt);
      console.log(" _loopCnt= ", _loopCnt);
    }

    function GoSort(sortFn) {
      var _ary = MarkRandomArray();
      console.log(_ary);
      console.log("----------Sort----------------");
      console.log("----------Start----------------");
      sortFn(_ary);
      console.log("----------END----------------");
      console.log(_ary);
    }


  </script>
</head>
<body>
  <button onclick="GoSort(SelectSort)">选择排序</button>
  <button onclick="GoSort(PopuSort)">冒泡排序</button>
  <button onclick="GoSort(InsertSort)">插入排序</button>
</body>

  

 posted on 2016-03-14 18:11  Grart  阅读(313)  评论(0编辑  收藏  举报