text: function bubbleSort2(arr) {
      console.time('改进后冒泡排序耗时');
      var i = arr.length-1;  //初始时,最后位置保持不变
      while ( i> 0) {
          var pos= 0; //每趟开始时,无记录交换
          for (var j= 0; j< i; j++)
              if (arr[j]> arr[j+1]) {
                  pos= j; //记录交换的位置
                  var tmp = arr[j]; arr[j]=arr[j+1];arr[j+1]=tmp;
              }
          i= pos; //为下一趟排序作准备
       }
       console.timeEnd('改进后冒泡排序耗时');
       return arr;
  }
  var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48];
  console.log(bubbleSort2(arr));
  
  //选择排序
  function selectionSort(arr) {
      var len = arr.length;
      var minIndex, temp;
      console.time('选择排序耗时');
      for (var i = 0; i < len - 1; i++) {
          minIndex = i;
          for (var j = i + 1; j < len; j++) {
              if (arr[j] < arr[minIndex]) {     //寻找最小的数
                  minIndex = j;                 //将最小数的索引保存
              }
          }
          temp = arr[i];
          arr[i] = arr[minIndex];
          arr[minIndex] = temp;
      }
      console.timeEnd('选择排序耗时');
      return arr;
  }
  var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48];
  console.log(selectionSort(arr));
  
  //希尔排序
  function shellSort(arr) {
      var len = arr.length,
          temp,
          gap = 1;
      console.time('希尔排序耗时:');
      while(gap < len/5) {          //动态定义间隔序列
          gap =gap*5+1;
      }
      for (gap; gap > 0; gap = Math.floor(gap/5)) {
          for (var i = gap; i < len; i++) {
              temp = arr[i];
              for (var j = i-gap; j >= 0 && arr[j] > temp; j-=gap) {
                  arr[j+gap] = arr[j];
              }
              arr[j+gap] = temp;
          }
      }
      console.timeEnd('希尔排序耗时:');
      return arr;
  }
  var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48];
  console.log(shellSort(arr));
  
  //归并排序
  function mergeSort(arr) {  //采用自上而下的递归方法
      var len = arr.length;
      if(len < 2) {
          return arr;
      }
      var middle = Math.floor(len / 2),
          left = arr.slice(0, middle),
          right = arr.slice(middle);
      return merge(mergeSort(left), mergeSort(right));
  }
  
  function merge(left, right)
  {
      var result = [];
      console.time('归并排序耗时');
      while (left.length && right.length) {
          if (left[0] <= right[0]) {
              result.push(left.shift());
          } else {
              result.push(right.shift());
          }
      }
  
      while (left.length)
          result.push(left.shift());
  
      while (right.length)
          result.push(right.shift());
      console.timeEnd('归并排序耗时');
      return result;
  }
  var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48];
  console.log(mergeSort(arr));
  
  
  
  //插入排序
  function insertionSort(array) {
      if (Object.prototype.toString.call(array).slice(8, -1) === 'Array') {
          console.time('插入排序耗时：');
          for (var i = 1; i < array.length; i++) {
              var key = array[i];
              var j = i - 1;
              while (j >= 0 && array[j] > key) {
                  array[j + 1] = array[j];
                  j--;
              }
              array[j + 1] = key;
          }
          console.timeEnd('插入排序耗时：');
          return array;
      } else {
          return 'array is not an Array!';
      }
  }
  