具体代码及比较如下所示: 复制代码 代码如下: 常见排序算法 之 JavaScript版 <br> Array.prototype.swap = function(i, j) <br> { <br> var temp = this[i]; <br> this[i] = this[j]; <br> this[j] = temp; <br> } <br> Array.prototype.bubbleSort = function() <br> { <br> for (var i = this.length - 1; i > 0; --i) <br> { <br> for (var j = 0; j < i; j) <BR> { <BR> if (this[j] > this[j 1]) this.swap(j, j 1); <br> } <br> } <br> } <br> Array.prototype.selectionSort = function() <br> { <br> for (var i = 0; i < this.length; i) <BR> { <BR> var index = i; <BR> for (var j = i 1; j < this.length; j) <BR> { <BR> if (this[j] < this[index]) index = j; <BR> } <BR> this.swap(i, index); <BR> } <BR> } <BR> Array.prototype.insertionSort = function() <BR> { <BR> for (var i = 1; i < this.length; i) <BR> { <BR> var j = i, value = this[i]; <BR> while (j > 0 && this[j - 1] > value) <br> { <br> this[j] = this[j - 1]; <br> --j; <br> } <br> this[j] = value; <br> } <br> } <br> Array.prototype.shellSort = function() <br> { <br> for (var step = this.length >> 1; step > 0; step >>= 1) <br> { <br> for (var i = 0; i < step; i) <BR> { <BR> for (var j = i step; j < this.length; j = step) <BR> { <BR> var k = j, value = this[j]; <BR> while (k >= step && this[k - step] > value) <br> { <br> this[k] = this[k - step]; <br> k -= step; <br> } <br> this[k] = value; <br> } <br> } <br> } <br> } <br> Array.prototype.quickSort = function(s, e) <br> { <br> if (s == null) s = 0; <br> if (e == null) e = this.length - 1; <br> if (s >= e) return; <br> this.swap((s e) >> 1, e); <br> var index = s - 1; <br> for (var i = s; i <= e; i) <BR> { <BR> if (this[i] <= this[e]) this.swap(i, index); <BR> } <BR> this.quickSort(s, index - 1); <BR> this.quickSort(index 1, e); <BR> } <BR> Array.prototype.stackQuickSort = function() <BR> { <BR> var stack = [0, this.length - 1]; <BR> while (stack.length > 0) <BR> { <BR> var e = stack.pop(), s = stack.pop(); <BR> if (s >= e) 继续; <BR> this.swap((s e) >> 1, e); <BR> var 索引 = s - 1; <BR> for (var i = s; i <= e; i) <BR> { <BR> if (this[i] <= this[e]) this.swap(i, index); <BR> } <BR> stack.push(s, 索引 - 1, 索引 1, e); <BR> } <BR> } <BR> Array.prototype.mergeSort = function(s, e, b) <BR> { <BR> if (s == null) s = 0; <BR> if (e == null) e = this.length - 1; <BR> if (b == null) b = new Array(this.length); <BR> if (s >= e) 返回; <BR> var m = (s e) >> 1; <BR> this.mergeSort(s, m, b); <BR> this.mergeSort(m 1, e, b); <BR> for (var i = s, j = s, k = m 1; i <= e; i) <BR> { <BR> b[i] = this[(k > e || j < ;=m&&这个[j]<这个[k])? j : k ]; <BR> } <BR> for (var i = s; i <= e; i) this[i] = b[i]; <BR> } <BR> Array.prototype.heapSort = function() <BR> { <BR> for (var i = 1; i <;this.length; i) <BR> { <BR> for (var j = i, k = (j - 1) >> 1; k >= 0; j = k, k = (k - 1) >> 1) <BR> { <BR> if (this[ k] >= this[j]) break; <BR> this.swap(j, k); <BR> } <BR> } <BR> for (var i = this.length - 1; i > 0; --i) <BR> { <BR> this.swap(0, i); <BR> for (var j = 0, k = (j 1) << 1; k <= i; j = k, k = (k 1) <<< 1) <BR> { <BR> if (k == i || this[k] < this[k - 1]) --k; <BR> if (this[k] <= this[j]) break; <BR> this.swap(j, k); <BR> } <BR> } <BR> } <BR> 函数generate() <BR> { <BR> var max = parseInt(txtMax.value), count = parseInt(txtCount.value); <BR> if (isNaN(max) || isNaN(count)) <BR> { <BR> alert("个数和顶峰必须是一个整数"); <BR> 返回; <BR> } <BR> var 数组 = []; <BR> for (var i = 0; i < count; i) array.push(Math.round(Math.random() * max)); <BR> txtInput.value = array.join("n"); <BR> txtOutput.value = “”; <BR> } <BR> 函数演示(类型) <BR> { <BR> var 数组 = txtInput.value == “” ? [] : txtInput.value.replace().split("n"); <BR> for (var i = 0; i < array.length; i) array[i] = parseInt(array[i]); <BR> var t1 = new Date(); <BR> eval(“数组。” 类型 “Sort()”); <BR> var t2 = new Date(); <BR> lblTime.innerText = t2.valueOf() - t1.valueOf(); <BR> txtOutput.value = array.join("n"); <BR> } <BR></脚本> <br></头> <br> <br><table> <br><tr> <br> <td> <br> <textarea id="txtInput" style="width:120px;height:500px;" 只读></textarea> <BR> </td> <br> </td> <td> <br> 随机数<input> 最大随机数<input> <button onclick="generate()">重新生成</button><br /><br /><br /><br /> <br> 运行(毫秒):<label></label><br /><br /><br /><br /> <br> <button>冒泡排序</button><br /><br /> <br> <button>选择排序</button><br /><br /> <br> <button>插入排序</button><br /><br /> <br> <button>谢尔排序</button><br /><br /> <br> <button>快速排序(梯度)</button><br /><br /> <br> <button>快速排序(堆栈)</button><br /><br /> <br> <button>归并排序</button><br /><br /> <br> <button>堆排序</button><br /><br /> <br> </td> <br> <td> <br> <textarea id="txtOutput" style="width:120px;height:500px;" 只读> <br> </td> <br></tr> <br></表> <br></身体> <br></html> <br> <br>快速排序、插入排序、希尔排序、冒泡排序、quickSort、insertSort、shellSort、bubbleSort、javascript排序<br><strong>说明<br></strong>写这个主要是为了锻炼自己,并无实际意义。<br>每个浏览器测试下面的数据都会不一样。比如我用chrome测试一般快速排序都能找到最快,IE则根据磁盘存储容量有可能希尔最快。<br>不要用手工数据去测试冒泡排序(浏览器崩溃了我不管)<br>如果有兴趣可以<a href="http://xiazai.jb51.net/201102/yuanma/js-code.rar" target="_blank">下载测试页面</a><br><br><strong>个人理解<br> <br></strong>冒泡排序:最简单,也最慢,显着长度小于7最<br>插入排序:比冒泡快,比快速排序和希尔排序慢,较小数据有优势 <br>快速排序:这是一种非常快的排序方式,V8的排序方法就使用快速排序和插入排序的结合<br>希尔排序:在非chrome下负载长度小于1000,希尔排序比快速排序更快<br>系统方法:在forfox下系统的这个方法非常快 <br><br><strong>算法源码</strong> <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="58196" class="copybut" id="copybut58196" onclick="doCopy('code58196')"><u>复制代码</u></a> </span> 代码如下:</div> <div class="codebody" id="code58196"> <br>// ---------- 一些排序算法 <br>// js 利用sort进行排序 <br>systemSort:function(array){ <br>return array.sort(function(a, b){ <br>return a - b; <br>}); <br>}, <br>// 冒泡排序 <br>bubbleSort:function(array){ <br>var i = 0, len = array.length, <br>j, d; <br>for(; i<len; i ){ <BR>for(j=0; j<len; j ){ <BR>if(array[i] < array[j]){ <BR>d = array[j]; <BR>array[j] = array[i]; <BR>array[i] = d; <BR>} <BR>} <BR>} <BR>return array; <BR>}, <BR>// 快速排序 <BR>quickSort:function(array){ <BR>//var array = [8,4,6,2,7,9,3,5,74,5]; <BR>//var array = [0,1,2,44,4,324,5,65,6,6,34,4,5,6,2,43,5,6,62,43,5,1,4,51,56,76,7,7,2,1,45,4,6,7]; <BR>var i = 0; <BR>var j = array.length - 1; <BR>var Sort = function(i, j){ <BR>// 结束条件 <BR>if(i == j ){ return }; <BR>var key = array[i]; <BR>var stepi = i; // 记录开始位置 <BR>var stepj = j; // 记录结束位置 <BR>while(j > i){ <br>// j <<-------------- 向前查找 <BR>if(array[j] >= key){ <br>j--; <br>}else{ <br>array[i] = array[j] <br>//i ------------>>向后查找 <br>while(j > i){ <br>if(array[i] > key){ <br>array[j] = array[i]; <br>break; <br>} <br>} <br>} <br>} <br>// 如果第一个取出的 key 是最小的数 <br>if(stepi == i){ <br>Sort( i, stepj); <br>return ; <br>} <br>// 最后一个空位留给 key <br>array[i] = key; <br>// 递归 <br>Sort(stepi, i); <br>Sort(j, stepj); <br>} <br>Sort(i, j); <br>return array; <br>}, <br>// 插入排序 <br>insertSort:function(array){ <br>// http://baike.baidu.com/image/d57e99942da24e5dd21b7080 <br>// http://baike.baidu.com/view/396887.htm <br>//var array = [0,1,2,44,4,324,5,65,6,6,34,4,5,6,2,43,5,6,62,43,5,1,4,51,56,76,7,7,2,1,45,4,6,7]; <br>var i = 1, j, step, key, <br>len = array.length; <br>for(; i < len; i ){ <BR>step = j = i; <BR>key = array[j]; <BR>while(--j > -1){ <br>if(array[j] > key){ <br>array[j 1] = array[j]; <br>}else{ <br>break; <br>} <br>} <br>array[j 1] = key; <br>} <br>return array; <br>}, <br>// 希尔排序 <br>//Jun.array.shellSort(Jun.array.df(10000)); <br>shellSort:function(array){ <br>// http://zh.wikipedia.org/zh/希尔排序 <br>// var array = [13,14,94,33,82,25,59,94,65,23,45,27,73,25,39,10]; <br>var stepArr = [1750, 701, 301, 132, 57, 23, 10, 4, 1]; // reverse() 在维基上看到这个最优的步长 较小数组 <br>//var stepArr = [1031612713, 217378076, 45806244, 9651787, 2034035, 428481, 90358, 19001, 4025, 836, 182, 34, 9, 1]//针对大数组的步长选择 <br>var i = 0; <br>var stepArrLength = stepArr.length; <br>var len = array.length; <br>var len2 = parseInt(len/2); <br>for(;i < stepArrLength; i ){ <BR>if(stepArr[i] > len2){ <br>continue; <br>} <br>stepSort(stepArr[i]); <br>} <br>// 排序一个步长 <br>function stepSort(step){ <br>//console.log(step) 使用的步长统计 <br>var i = 0, j = 0, f, tem, key; <br>var stepLen = len%step > 0 ? parseInt(len/step) 1 : len/step; <br><br>for(;i < step; i ){// 依次循环列 <BR>for(j=1;/*j < stepLen && */step * j i < len; j ){//依次循环每列的每行 <BR>tem = f = step * j i; <BR>key = array[f]; <BR>while((tem-=step) >= 0){// 依次向上查找 <br>if(array[tem] > key){ <br>array[tem step] = array[tem]; <br>}else{ <br>break; <br>} <br>} <br>array[tem step ] = key; <br>} <br>} <br>} <br>return array; <br>} <br> </div> <br><a href="http://xiazai.jb51.net/201102/yuanma/js-code.rar" target="_blank">测试代码打包下载</a> </td></button></button></label> </td> </table> </div>