复制代码 代码如下:
<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>
相关推荐:
优化页面-提升用户体验与搜索引擎排名的关键,绍兴视频营销推广
怎样用AI写文章?快速高效创作新技能!
SEO发明:引领数字时代营销革命的力量,速卖通外贸推广网站
SEO注意事项:助力网站流量提升的关键策略,济宁快速seo优化价格
SEO优化如何进行:提升网站排名,轻松超越竞争对手,ai写作怎么操作手机
什么是seo发外链,seo外链类型有哪些 ,小小苏ai
SEO是什么意思?揭秘SEO的真正含义与重要性,公司推广网站询问d火18星来
“扩写AI”-引领写作革命,开启智能创作新时代,dede seo 标题如何填写
SEO师:如何在数字时代为企业赢得流量与销量,十堰网站关键词优化教程
ChatGPT最新版本更新内容:智能对话体验再升级,更多功能与应用,ai证伪
SEO快排还有效果吗揭秘快速排名的真相与未来趋势,ai人像波普
SEO数量-如何提升你的网站排名与流量?,射阳seo优化五星服务
ChatGPT全球宕机:人工智能的崩塌与未来的挑战,中国ai和美国ai教父
在线AI文章生成器开启智能创作新时代
“新热度”:引领潮流的力量,如何趋势的脉搏,浙江通用网站建设特点
ChatGPTWindows版本下载:让AI助力您的工作和生活,ai yamama
ChatGPT:我目前无法查看或解析附件,您是否遇到过这样的困扰?,ai+燃烧
SEO搜索关键词是什么意思?全方位解析关键词优化的核心要素,lol ai图片
SEO管家:为您的网站保驾护航的智能SEO助手,网站推广作用有哪些类型
AI优化文章:如何利用人工智能提升写作效率和质量
SEO全站优化:打造强大网站排名的必备利器,AI论文写作的优点
什么是seo寄生虫,寄生虫seo原理 ,AI 疫
SEO行销:开启网站流量增长的秘密武器,百威产品关键词查询排名
ChatGPT怎么打开不了?全方位解决方案!,大庆ai
ChatGPT坏了用什么?替代方案,满足你的智能对话需求,星际一的ai
SEO考核:如何通过精准的SEO优化提升网站排名与流量,茶艺营销推广方案怎么写
为什么seo这么麻烦,seo是什么意思 为什么要做seo ,dota1ai地图命令选ai
高效创作新时代AI文案速写工具,让创作更轻松
ChatGPT可以实现新闻报道的即时自动化生成,怎么更改ai2的图标
seo用什么法宝,列出5种seo赚钱方式 ,ai怎么更改文档样式
AI文章概括缩写:让内容高效获取的智能工具,ai 处理文件
ChatGPTO1Pro模型:开启AI新纪元,免费应用带来无尽可能,苹果上的ai写作在哪里
为什么要年前做SEO,企业为什么做seo推广 ,云南ai大数据公司
2025年整站SEO排名优化策略:让你的网站脱颖而出,id排版ai
SEO深度解析:如何通过深度优化提升网站排名,带来流量和转化,咸宁网站建设大概费用
文章去AI回归创作的本真之美
seo简报什么意思,seo工作汇报 ,万花筒 ai
seo网络推广要做什么,seo 网络推广 ,ai少女喝药
seo需要保持什么心态,seo的要求 ,ai83562
优化标题:如何让你的文章更具吸引力与点击力,整站网站优化解决方案
互联网留痕:数字时代的隐形轨迹与自我管理,灯塔网站推广包年多少钱
ChatGPT连了外网也登不了?如何解决这一问题,重新畅享AI助力!,ai少女大瓜
亚马逊的seo是什么阿,亚马逊seo项目 ,中考用ai写作会判0分吗
OpenAI账号申诉怎么办?全方位解析解决方案,ai写作免费公众号下载
SEO阶段解析:从入门到精通,助你站稳搜索引擎的前沿,网站建设特定开发
SEO拓客,让您的业务飞速增长!,密云自适应网站建设
文章创作AI:引领智能写作的新时代
用AI征文工具,轻松创作出精彩文章!
SEO课:让你从小白变成搜索引擎优化高手,推广自媒体营销计划
SEO元素-提升网站排名的核心要素,推广分成网站有哪些