复制代码 代码如下:
<script>
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("\n");
txtOutput.value = "";
}

function demo(type)
{
var array = txtInput.value == "" ? [] : txtInput.value.replace().split("\n");
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("\n");
}
</script>

<body onload=generate()>
<table style="width:100%;height:100%;font-size:12px;font-family:宋体">
<tr>
<td align=right>
<textarea id=txtInput readonly style="width:100px;height:100%"></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 onclick=generate()>重新生成</button><br><br><br><br>
耗时(毫秒):<label id=lblTime></label><br><br><br><br>
<button onclick=demo("bubble")>冒泡排序</button><br><br>
<button onclick=demo("selection")>选择排序</button><br><br>
<button onclick=demo("insertion")>插入排序</button><br><br>
<button onclick=demo("shell")>谢尔排序</button><br><br>
<button onclick=demo("quick")>快速排序(递归)</button><br><br>
<button onclick=demo("stackQuick")>快速排序(堆栈)</button><br><br>
<button onclick=demo("merge")>归并排序</button><br><br>
<button onclick=demo("heap")>堆排序</button><br><br>
</td>
<td align=left>
<textarea id=txtOutput readonly style="width:100px;height:100%"></textarea>
</td>
</tr>
</table>
</body>
标签:
常用排序

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“js常用排序实现代码”
暂无“js常用排序实现代码”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。