标签:dex console ini ted div 描述 排序 ret selection
冒泡排序是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。
<script type="text/javascript">
var BubbleSort ={};
BubbleSort.arrary =[];
BubbleSort.init =function(){
this.arrary.length =0;
this.arrary.push(3);
this.arrary.push(1);
this.arrary.push(44);
this.arrary.push(38);
this.arrary.push(5);
this.arrary.push(47);
this.arrary.push(15);
this.arrary.push(50);
this.arrary.push(48);
this.arrary.push(46);
this.arrary.push(27);
this.arrary.push(26);
this.arrary.push(36);
}
BubbleSort.show =function(){
for(var i=0;i<this.arrary.length;i++)
{
console.log(this.arrary[i]);
}
}
BubbleSort.bubbleSort = function(arr)
{
var len = arr.length;
var minIndex ,temp;
for(var i=0;i<len-1;i++){
for(var j=0;j<len-1-i;j++)
{
if(arr[j]>arr[j+1])
{
var temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] =temp;
}
}
}
return arr;
}
BubbleSort.init();
console.log("unsorted arry");
BubbleSort.show();
console.log("sorted arry");
BubbleSort.arrary = BubbleSort.selectionSort(BubbleSort.arrary);
BubbleSort.show();
</script>
标签:dex console ini ted div 描述 排序 ret selection
原文地址:https://www.cnblogs.com/ms_senda/p/11391706.html