javascript冒泡排序小结_基础知识
程序员文章站
2022-04-01 11:05:19
...
冒泡排序示例,双向冒泡排序与对双向冒泡排序稍微的改进的可视化效果。
代码很简单,不知道有木有未知bug。
大神请勿吐槽
冒泡排序示例
var ls=[ 98,13,6,25,38,36,30,44,38,80,61,28,47,34,95,18,85,58,89,85,42,61,74,35,13,14,80,7,10,44,10,47,13,11,52,25,24,48,34,12,88,80,33,8,80,45,64,52,79,77 ]; for(var i=0;ils[j]){ ls[i]=ls[i]+ls[j]; ls[j]=ls[i]-ls[j]; ls[i]=ls[i]-ls[j]; } } }
双向冒泡排序示例
var ls=[ 6,13,98,25,38,36,30,44,38,80,61,28,47,34,95,18,85,58,89,85,42,61,74,35,13,14,80,7,10,44,10,47,13,11,52,25,24,48,34,12,88,80,33,8,80,45,64,52,79,77 ]; for(var i=0;ils[j]){ ls[i]=ls[i]+ls[j]; ls[j]=ls[i]-ls[j]; ls[i]=ls[i]-ls[j]; }//前面的比较 } }
双向冒泡排序稍微改进示例
var ls=[ 98,13,6,25,38,36,30,44,38,80,61,28,47,34,95,18,85,58,89,85,42,61,74,35,13,14,80,7,10,44,10,47,13,11,52,25,24,48,34,12,88,80,33,8,80,45,64,52,79,77 ]; var lent=ls.length; for(var i=0;ils[j+1]){ ls[i*2]=ls[i*2]+ls[j+1]; ls[j+1]=ls[i*2]-ls[j+1]; ls[i*2]=ls[i*2]-ls[j+1]; }//保持内层第一个数为循环最小 if(ls[lent-i*2-1] ls[j+1]){ ls[i*2+1]=ls[i*2+1]+ls[j+1]; ls[j+1]=ls[i*2+1]-ls[j+1]; ls[i*2+1]=ls[i*2+1]-ls[j+1]; }//下一个 } }
上一篇: 文档型数据库设计模式