常见排序算法 之 Javascript版

用JavaScript实现的常见排序算法:冒泡排序,选择排序,插入排序,谢尔排序,快速排序(递归),快速排序(堆栈),归并排序,堆排序。
具体代码及比较如下所示:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<title> 常见排序算法 之 JavaScript版 </title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="keywords" content="排序,算法,JavaScript排序" />
<meta name="description" content="用JavaScript实现的常见排序算法:冒泡排序,选择排序,插入排序,谢尔排序,快速排序(递归),快速排序(堆栈),归并排序,堆排序" />
<script type="text/javascript">
 Array.prototype.swap = function(i, j)
 {
 var temp = this[i];
 this[i] = this[j];
 this[j] = temp;
 }
 Array.prototype.bubbleSort = function()
 {
 for (var i = this.length - 1; i > 0; --i)
 {
 for (var j = 0; j < i; ++j)
 {
 if (this[j] > this[j + 1]) this.swap(j, j + 1);
 }
 }
 }
 Array.prototype.selectionSort = function()
 {
 for (var i = 0; i < this.length; ++i)
 {
 var index = i;
 for (var j = i + 1; j < this.length; ++j)
 {
 if (this[j] < this[index]) index = j;
 }
 this.swap(i, index);
 }
 }
 Array.prototype.insertionSort = function()
 {
 for (var i = 1; i < this.length; ++i)
 {
 var j = i, value = this[i];
 while (j > 0 && this[j - 1] > value)
 {
 this[j] = this[j - 1];
 --j;
 }
 this[j] = value;
 }
 }
 Array.prototype.shellSort = function()
 {
 for (var step = this.length >> 1; step > 0; step >>= 1)
 {
 for (var i = 0; i < step; ++i)
 {
 for (var j = i + step; j < this.length; j += step)
 {
 var k = j, value = this[j];
 while (k >= step && this[k - step] > value)
 {
 this[k] = this[k - step];
 k -= step;
 }
 this[k] = value;
 }
 }
 }
 }
 Array.prototype.quickSort = function(s, e)
 {
 if (s == null) s = 0;
 if (e == null) e = this.length - 1;
 if (s >= e) return;
 this.swap((s + e) >> 1, e);
 var index = s - 1;
 for (var i = s; i <= e; ++i)
 {
 if (this[i] <= this[e]) this.swap(i, ++index);
 }
 this.quickSort(s, index - 1);
 this.quickSort(index + 1, e);
 }
 Array.prototype.stackQuickSort = function()
 {
 var stack = [0, this.length - 1];
 while (stack.length > 0)
 {
 var e = stack.pop(), s = stack.pop();
 if (s >= e) continue;
 this.swap((s + e) >> 1, e);
 var index = s - 1;
 for (var i = s; i <= e; ++i)
 {
 if (this[i] <= this[e]) this.swap(i, ++index);
 }
 stack.push(s, index - 1, index + 1, e);
 }
 }
 Array.prototype.mergeSort = function(s, e, b)
 {
 if (s == null) s = 0;
 if (e == null) e = this.length - 1;
 if (b == null) b = new Array(this.length);
 if (s >= e) return;
 var m = (s + e) >> 1;
 this.mergeSort(s, m, b);
 this.mergeSort(m + 1, e, b);
 for (var i = s, j = s, k = m + 1; i <= e; ++i)
 {
 b[i] = this[(k > e || j <= m && this[j] < this[k]) ? j++ : k++];
 }
 for (var i = s; i <= e; ++i) this[i] = b[i];
 }
 Array.prototype.heapSort = function()
 {
 for (var i = 1; i < this.length; ++i)
 {
 for (var j = i, k = (j - 1) >> 1; k >= 0; j = k, k = (k - 1) >> 1)
 {
 if (this[k] >= this[j]) break;
 this.swap(j, k);
 }
 }
 for (var i = this.length - 1; i > 0; --i)
 {
 this.swap(0, i);
 for (var j = 0, k = (j + 1) << 1; k <= i; j = k, k = (k + 1) << 1)
 {
 if (k == i || this[k] < this[k - 1]) --k;
 if (this[k] <= this[j]) break;
 this.swap(j, k);
 }
 }
 }
 function generate()
 {
 var max = parseInt(txtMax.value), count = parseInt(txtCount.value);
 if (isNaN(max) || isNaN(count))
 {
 alert("个数和最大值必须是一个整数");
 return;
 }
 var array = [];
 for (var i = 0; i < count; ++i) array.push(Math.round(Math.random() * max));
 txtInput.value = array.join(" ");
 txtOutput.value = "";
 }
 function demo(type)
 {
 var array = txtInput.value == "" ? [] : txtInput.value.replace().split(" ");
 for (var i = 0; i < array.length; ++i) array[i] = parseInt(array[i]);
 var t1 = new Date();
 eval("array." + type + "Sort()");
 var t2 = new Date();
 lblTime.innerText = t2.valueOf() - t1.valueOf();
 txtOutput.value = array.join(" ");
 }
</script>
</head>
<body οnlοad="generate();">
<table style="font-size:12px;">
<tr>
 <td align="right">
 <textarea id="txtInput" style="width:120px;height:500px;" readonly></textarea>
 </td>
 <td width="150" align="center">
 随机数个数<input id="txtCount" value="500" style="width:50px" /><br /><br />
 最大随机数<input id="txtMax" value="1000" style="width:50px" /><br /><br />
 <button οnclick="generate()">重新生成</button><br /><br /><br /><br />
 耗时(毫秒):<label id="lblTime"></label><br /><br /><br /><br />
 <button οnclick="demo('bubble');">冒泡排序</button><br /><br />
 <button οnclick="demo('selection');">选择排序</button><br /><br />
 <button οnclick="demo('insertion');">插入排序</button><br /><br />
 <button οnclick="demo('shell');">谢尔排序</button><br /><br />
 <button οnclick="demo('quick');">快速排序(递归)</button><br /><br />
 <button οnclick="demo('stackQuick');">快速排序(堆栈)</button><br /><br />
 <button οnclick="demo('merge');">归并排序</button><br /><br />
 <button οnclick="demo('heap');">堆排序</button><br /><br />
 </td>
 <td align="left">
 <textarea id="txtOutput" style="width:120px;height:500px;" readonly></textarea>
 </td>
</tr>
</table>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值