欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaScript数组基于交换的排序示例【冒泡排序】

程序员文章站 2022-05-27 12:03:29
本文实例讲述了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>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>www.jb51.net js冒泡排序</title>
</head>
<body>
<script>
var array1=new array(13,55,37,45,9,60,21,10);
document.write("没有交换前 "+array1+"<br>");
var temp;
for(a in array1){
for(b in array1){
if(array1[a]<array1[b]){
temp=array1[a];
array1[a]=array1[b];
array1[b]=temp;
}
}
}
document.write("交换之后的 "+array1);
</script>
</body>
</html>

使用在线html/css/javascript代码运行工具: http://tools.jb51.net/code/htmljsrun,测试运行结果:

 JavaScript数组基于交换的排序示例【冒泡排序】

ps:这里再为大家推荐一款关于排序的演示工具供大家参考:

在线动画演示插入/选择/冒泡/归并/希尔/快速排序算法过程工具:

更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript数学运算用法总结》、《javascript数据结构与算法技巧总结》、《javascript数组操作技巧总结》、《javascript排序算法总结》、《javascript遍历算法与技巧总结》、《javascript查找算法技巧总结》及《javascript错误与调试技巧总结

希望本文所述对大家javascript程序设计有所帮助。