归并排序与快速排序比较(JS版)

具体解释看代码,代码如下

   1:  //主函数
   2:  function main() {
   3:      var quickTimeArray = new Array();
   4:      var mergeTimeArray = new Array();
   5:      for (var i = 1; i <= 20; i++) {
   6:   
   7:          
   8:          var array = getRandomArrayEasy(5000*i    );
   9:          var array2 = array.slice(0);
  10:          
  11:   
  12:          var startTime = new Date().getTime();
  13:          doMergeSort(array);
  14:          
  15:          
  16:          var endTime = new Date().getTime();
  17:          mergeTimeArray.push(endTime - startTime);
  18:   
  19:   
  20:   
  21:          var startTime = new Date().getTime();
  22:          
  23:          doQuickSort(array2);
  24:          
  25:   
  26:          var endTime = new Date().getTime();
  27:          
  28:          quickTimeArray.push(endTime - startTime);
  29:          
  30:      }
  31:      draw(quickTimeArray,mergeTimeArray);
  32:   
  33:   
  34:  }
  35:   
  36:   
  37:   
  38:  //归并排序
  39:  function doMergeSort(array) {
  40:      mergeSort(array, 0, array.length - 1);
  41:  }
  42:   
  43:  function mergeSort(array, low, high) {
  44:      var mid = 0;
  45:      if (low < high) {
  46:          mid = parseInt((low + high) / 2);
  47:          mergeSort(array, low, mid);
  48:          mergeSort(array, mid + 1, high);
  49:          merge(array, low, mid, high);
  50:      }
  51:  }
  52:   
  53:  function merge(array, low, mid, high) {
  54:      var tempArray = new Array();
  55:      var i = low;
  56:      var j = mid + 1;
  57:      while (true) {
  58:          var small = 0;
  59:          if (array[i] < array[j]) {
  60:              small = array[i]
  61:              i++;
  62:          } else {
  63:              small = array[j];
  64:              j++;
  65:          }
  66:          tempArray.push(small);
  67:          if (i > mid) {
  68:              //i结束,只需要复制i到j-1就行了
  69:              for (var k = low; k < j; k++) {
  70:                  array[k] = tempArray.shift();
  71:              }
  72:              break;
  73:          }
  74:          if (j > high) {
  75:              for (var k = i; k <= mid; k++) {
  76:                  tempArray.push(array[k]);
  77:              }
  78:              for (var k = low; k <= high; k++) {
  79:                  array[k] = tempArray.shift();
  80:              }
  81:              break;
  82:          }
  83:   
  84:      }
  85:   
  86:  }
  87:   
  88:  //快速排序
  89:  //return 排序好的数组
  90:  function doQuickSort(array) {
  91:      quickSort(array, 0, array.length - 1);
  92:  }
  93:   
  94:  function quickSort(array, low, high) {
  95:      var w = 0;
  96:      if (low < high) {
  97:          w = split(array, low, high);
  98:          quickSort(array, low, w - 1);
  99:          quickSort(array, w + 1, high);
 100:      }
 101:  }
 102:   
 103:  //快速排序的划分算法
 104:  //return 划分好的数组
 105:  function split(array, low, high) {
 106:      var temp = array[low];
 107:      var temp2 = getSplitingElementIndex(array, low, high);
 108:      array[low] = array[temp2];
 109:      //获得划分元素的位置
 110:      array[temp2] = temp;
 111:      var i = low;
 112:      var x = array[low];
 113:      for (var j = low + 1; j <= high; j++) {
 114:          if (array[j] <= x) {
 115:              i++;
 116:              if (i != j) {
 117:                  var temp3 = array[i];
 118:                  array[i] = array[j];
 119:                  array[j] = temp3;
 120:              }
 121:          }
 122:      }
 123:      var temp4 = array[low];
 124:      array[low] = array[i];
 125:      array[i] = temp4;
 126:      return i;
 127:   
 128:  }
 129:   
 130:  //寻找划分元素
 131:  //return 划分元素在数组中的位置
 132:  //param array 数组
 133:  //param low 下限
 134:  //param high 上限
 135:  function getSplitingElementIndex(array, low, high) {
 136:      var mid = low + parseInt((low + high) / 2);
 137:      if (array[mid] > array[low])//array[mid] 为第二大时返回mid
 138:      {
 139:          if (array[mid] < array[high]) {
 140:              return mid;
 141:          } else {
 142:              if (array[high] < array[low]) {
 143:                  return low;
 144:              } else {
 145:                  return high;
 146:              }
 147:          }
 148:      } else {
 149:          if (array[low] < array[high])//array[low] 为第二大是返回low
 150:          {
 151:              return low;
 152:          } else {
 153:              if (array[high] > array[low]) {
 154:                  return high;
 155:              } else {
 156:                  return low;
 157:              }
 158:          }
 159:      }
 160:  }
 161:   
 162:  //size数组大小
 163:  //floor数据下限 floor>=0;
 164:  //upper数据上限
 165:  //return 数组对象
 166:  function getRandomArray(size, floor, upper) {
 167:      if (floor < 0 || upper < 0)
 168:          return null;
 169:      var temp = upper - floor;
 170:      var array = new Array();
 171:      for (var i = 0; i < size; i++) {
 172:          array[i] = parseInt(Math.random() * temp + floor);
 173:      }
 174:      return array;
 175:   
 176:  }
 177:   
 178:  //封装好的默认范围为0到100000的getRandomArray
 179:  function getRandomArrayEasy(size) {
 180:      return getRandomArray(size, 0, 100000);
 181:  }
 182:   
 183:  //画图
 184:  function draw(QTArray,MTArray) {
 185:      var quickTr = document.getElementById("quickSort");
 186:      var mergeTr = document.getElementById("mergeSort");
 187:      for(var i=0;i<QTArray.length;i++)
 188:      {
 189:          var th  = document.createElement('th');
 190:          th.innerHTML=QTArray[i];
 191:          quickTr.appendChild(th);
 192:          
 193:          var th2  = document.createElement('th');
 194:          th2.innerHTML=MTArray[i];
 195:          mergeTr.appendChild(th2);
 196:      }
 197:   
 198:  }
 199:   
 200:   

附上调用的页面

 

   1:  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   2:  "http://www.w3.org/TR/html4/strict.dtd">
   3:   
   4:  <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
   5:      <head>
   6:          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   7:          <title>index</title>
   8:          <meta name="author" content="Jabba" />
   9:          <!-- Date: 2012-11-15 -->
  10:      </head>
  11:      <body >
  12:          <script type="text/javascript" src="sort.js"></script>
  13:          <div align= "center">
  14:              <input type="button" value="start" onclick="main()"/>
  15:          </div>
  16:          <div align="center">
  17:              <table id="table" rows="3" cols="21" border="1">
  18:                  <tr>
  19:                      <th>排序算法</th>
  20:                      <th colspan="20">排序时间(毫秒)</th>
  21:                  </tr>
  22:                  <tr id="mergeSort">
  23:                      <td>归并排序</td>
  24:                  </tr>
  25:                  <tr id="quickSort">
  26:                      <td>快速排序</td>
  27:                  </tr>
  28:              </table>
  29:          </div>
  30:   
  31:      </body>
  32:   
  33:  </html>
  34:   
    原文作者:@47
    原文地址: https://www.cnblogs.com/Jabba93/archive/2013/02/20/2919862.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞