js常用排序实现代码
2023-06-13 09:14:26 时间
<script>
Array.prototype.swap=function(i,j)
{
vartemp=this[i];
this[i]=this[j];
this[j]=temp;
}
Array.prototype.bubbleSort=function()
{
for(vari=this.length-1;i>0;--i)
{
for(varj=0;j<i;++j)
{
if(this[j]>this[j+1])this.swap(j,j+1);
}
}
}
Array.prototype.selectionSort=function()
{
for(vari=0;i<this.length;++i)
{
varindex=i;
for(varj=i+1;j<this.length;++j)
{
if(this[j]<this[index])index=j;
}
this.swap(i,index);
}
}
Array.prototype.insertionSort=function()
{
for(vari=1;i<this.length;++i)
{
varj=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(varstep=this.length>>1;step>0;step>>=1)
{
for(vari=0;i<step;++i)
{
for(varj=i+step;j<this.length;j+=step)
{
vark=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);
varindex=s-1;
for(vari=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()
{
varstack=[0,this.length-1];
while(stack.length>0)
{
vare=stack.pop(),s=stack.pop();
if(s>=e)continue;
this.swap((s+e)>>1,e);
varindex=s-1;
for(vari=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=newArray(this.length);
if(s>=e)return;
varm=(s+e)>>1;
this.mergeSort(s,m,b);
this.mergeSort(m+1,e,b);
for(vari=s,j=s,k=m+1;i<=e;++i)
{
b[i]=this[(k>e||j<=m&&this[j]<this[k])?j++:k++];
}
for(vari=s;i<=e;++i)this[i]=b[i];
}
Array.prototype.heapSort=function()
{
for(vari=1;i<this.length;++i)
{
for(varj=i,k=(j-1)>>1;k>=0;j=k,k=(k-1)>>1)
{
if(this[k]>=this[j])break;
this.swap(j,k);
}
}
for(vari=this.length-1;i>0;--i)
{
this.swap(0,i);
for(varj=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);
}
}
}
functiongenerate()
{
varmax=parseInt(txtMax.value),count=parseInt(txtCount.value);
if(isNaN(max)||isNaN(count))
{
alert("个数和最大值必须是一个整数");
return;
}
vararray=[];
for(vari=0;i<count;++i)array.push(Math.round(Math.random()*max));
txtInput.value=array.join("\n");
txtOutput.value="";
}
functiondemo(type)
{
vararray=txtInput.value==""?[]:txtInput.value.replace().split("\n");
for(vari=0;i<array.length;++i)array[i]=parseInt(array[i]);
vart1=newDate();
eval("array."+type+"Sort()");
vart2=newDate();
lblTime.innerText=t2.valueOf()-t1.valueOf();
txtOutput.value=array.join("\n");
}
</script>
<bodyonload=generate()>
<tablestyle="width:100%;height:100%;font-size:12px;font-family:宋体">
<tr>
<tdalign=right>
<textareaid=txtInputreadonlystyle="width:100px;height:100%"></textarea>
</td>
<tdwidth=150align=center>
随机数个数<inputid=txtCountvalue=500style="width:50px"><br><br>
最大随机数<inputid=txtMaxvalue=1000style="width:50px"><br><br>
<buttononclick=generate()>重新生成</button><br><br><br><br>
耗时(毫秒):<labelid=lblTime></label><br><br><br><br>
<buttononclick=demo("bubble")>冒泡排序</button><br><br>
<buttononclick=demo("selection")>选择排序</button><br><br>
<buttononclick=demo("insertion")>插入排序</button><br><br>
<buttononclick=demo("shell")>谢尔排序</button><br><br>
<buttononclick=demo("quick")>快速排序(递归)</button><br><br>
<buttononclick=demo("stackQuick")>快速排序(堆栈)</button><br><br>
<buttononclick=demo("merge")>归并排序</button><br><br>
<buttononclick=demo("heap")>堆排序</button><br><br>
</td>
<tdalign=left>
<textareaid=txtOutputreadonlystyle="width:100px;height:100%"></textarea>
</td>
</tr>
</table>
</body>
相关文章
- js 实现选择排序及优化
- js中四舍五入的方法_JS取整
- 多重排序 js「建议收藏」
- iOS小技能:WKWebView与JS的交互
- 原生 JS 实现 HTML 转 Markdown,以及其实现逻辑(html2md.js 或 html2markdown.js)
- Node.js中的关注点分离
- js test()详解编程语言
- JS动态引入js、CSS动态创建script/link/style标签详解编程语言
- JS数组排序:使用reverse()和sort()方法
- 使用JS连接MySQL数据库:实现化繁为简(js连接mysql数据库)
- Linux上的JS压缩工具(js压缩工具linux)
- 快速掌握JS操作MySQL数据库技巧(js操作mysql数据库)
- 使用JS实现Redis数据读取(js读取redis)
- 用SQL Server和JS实现数据库管理技术(sqlserver.js)
- JS技术连接Oracle数据库实现数据交互(js连接oracle实例)
- JS函数验证总结(方便js客户端输入验证)
- html中table数据排序的js代码
- 远离JS灾难css灾难之js私有函数和css选择器作为容器
- js对数组中的数字从小到大排序实现代码
- Js日期选择器并自动加入到输入框中示例代码
- js页面跳转的问题(跳转到父页面、最外层页面、本页面)
- js函数返回多个返回值的示例代码
- node.js中实现同步操作的3种实现方法
- js单独获取一个checkbox看其是否被选中