js常用排序实现代码
发布时间:2023-08-30 08:50:24
标签:常用排序
<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>


猜你喜欢
- 从控制台输入要出的拳 —— 剪刀(0)/石头(1)/布(2) 电脑 **随机*
- 1. 类的继承与方法的重载上面就是先定义了一个类A,然后由定义了一个类B,B继承了类A,这样B就有了A的非私有属性和方法。class Was
- 一、函数初识1、定义:将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可。2、好处:代码重用;保持一致性
- 1、打开文件open()函数简介 :打开文件使用open函数,可以打开一个已经存在的文件,如果没有这个文件的话,会创建一个新文件完整的语法格
- 样例:1.创建vue项目,不再详述2.引入vant之前用过很多插件做这个功能,但是效果都不尽人意,出现各种问题,直到遇到vant这个插件,完
- 使用Python加载最新的Excel读取类库xlwings可以说是Excel数据处理的利器,但使用起来还是有一些注意事项,否则高大上的Pyt
- 前言Python 开发者可能都听说过鸭子类型和猴子补丁这两个词,即使没听过,也大概率写过相关的代码,只不过并不了解其背后的技术要点是这两个词
- 一、re.compile 函数作用:compile 函数用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match()
- 解决中文乱码问题项目地址 github: https://github.com/Delgan/loguru文档:https://loguru
- 只是粗略的知道yield可以用来为一个函数返回值塞数据,比如下面的例子:def addlist(alist): &nbs
- 如果不是因为总监审查严格,一定要求这个细节解决掉,也许我也不会去深究根源性的解决办法,再此感谢MTIME负责而严格的同事。首先描述一下问题:
- Sodium 扩展在 PHP7.2 后是跟随 PHP 源码一起发布的,只需要在编译的时候加上 --with-sodium 即可安装成功。如果
- 我完成了更新我们在 Neutron的实时收入统计。在我花了一周的时间完成并且更新了我们的PHP脚本之后,我最终认决定开始使用Py
- 继续flask的学习之旅。今天介绍flask的登陆管理模块,还记得上一篇中的blog小项目么,登录是咱们自己写的验证代码,大概有以下几个步骤
- pyc的破解相对容易,使用cython将python文件编译成.so文件,能在一定程度上增强python源码的私密性。编译成.so文件环境准
- enumerate函数用于遍历序列中的元素以及它们的下标。enumerate函数说明:函数原型:enumerate(sequence, [s
- javascript时间戳和日期字符串相互转换代码(超简单)<html xmlns="http://www.w3.org/1
- 1. 索引及切片数组中的元素可以通过索引以及切片的手段进行访问或者修改,和列表的切片操作一样。下面直接使用代码进行实现,具体操作方式以及意义
- 引言众所周知,在python中我们用try…except…来捕获异常,使用raise来抛出异常,
- 一、软件下载官方下载地址:https://download.jetbrains.8686c.com/webstorm/WebStorm-20