使用javascript进行冒泡排序程序:无法正常工作 [英] bubble sort program using javascript:not working correctly

查看:110
本文介绍了使用javascript进行冒泡排序程序:无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<html>
<head>
<script language="javascript">
function bubble()
{
var a=new array();
var b=new array();
var temp;
b=prompt("enter values","0");
a=b.split(",");
for(i=0;i<=9;i++)
  {
  for(j=i+1;j<=9;j++)
     {
       temp=a[i];
       a[i]=a[j];
       a[j]=temp;
     }
   }
for(i=0;i<=9;i++)
   {
       alert(a[i]);
   }
       
}
bubble();
</script>
</head>
</html>

推荐答案

您错过了主要条件部分. :doh:
You missed the main condition part. :doh:
if(a[j]>a[j+1])
{
    temp=a[j+1];
    a[j+1]=a[j];
    a[j]=temp;
}



参考: http://www.metalshell.com/source_code/112/Javascript_Bubble_Sort.html [ ^ ]



Ref:http://www.metalshell.com/source_code/112/Javascript_Bubble_Sort.html[^]


这篇关于使用javascript进行冒泡排序程序:无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆