//冒泡排序
function bubble_sort(array){
var arr = array.concat();
var len = arr.length;
for(var i = len; i > 0; i--){
var key = 0;
while(key < i){
if(arr[key] > arr[key+1]){
var temp = arr[key];
arr[key] = arr[key+1];
arr[key+1] = temp;
}
key++;
}
}
alert(arr);
}
//改进1:没有发生交换则代表排序完成
function bubble_sort_1(array){
var arr = array.concat();
var len = arr.length;
var flag;
for(var i = len; i > 0; i--){
var key = 0;
flag = 0;
while(key < i){
if(arr[key] > arr[key+1]){
var temp = arr[key];
arr[key] = arr[key+1];
arr[key+1] = temp;
flag = 1;
}
key++;
}
if(flag == 0){break;}
}
alert(arr);
}
//改进:一趟同时冒泡大小两种
function bubble_sort_2(array){
var arr = array.concat();
var len = arr.length;
var front = 0;
var back = len - 1;
while(front < back){
for(var i = front; i < back; i++){
if(arr[i] > arr[i+1]){
var temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
back--;
for(var j = back; j >= 0; j--){
if(arr[j-1] > arr[j]){
var temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
front++;
}
alert(arr);
}