Javascript实现冒泡排序

javascript,sort
Thu Aug 16 2018 12:06:51 GMT+0000 (UTC)
function BubbleSort1(arr){
	var i, j, temp;

	for(i = 0; i < arr.length - 1; i++){
		for(j = 0; j < arr.length - 1; j++){
			if(arr[j] > arr[j+1]){
				temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
			}
			console.log(i+':'+j);
			console.log(arr);
		}
		console.log(i);
		console.log(arr);
		console.log('-------------')
	}
	
	return arr;
}
var arr = [ 5,9,8,2,4]
var result = BubbleSort1(arr); 


function BubbleSort2(arr){
	var i, j, temp;

	for(i = 0; i < arr.length - 1; i++){
		for(j = 0; j < arr.length - i - 1; j++){
			if(arr[j] > arr[j+1]){
				temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
			}
			console.log(i+':'+j);
			console.log(arr);
		}
		console.log(i);
		console.log(arr);
		console.log('-------------')
	}
	
	return arr;
}
var arr = [ 5,9,8,2,4]
var result = BubbleSort2(arr);


function BubbleSort3(arr){
	var i, j, temp;

	for(i = arr.length; i > 0; i--){
		for(j = 0; j < i - 1; j++){
			if(arr[j] > arr[j+1]){
				temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
			}
			console.log(i+':'+j);
			console.log(arr);
		}
		console.log(i);
		console.log(arr);
		console.log('-------------')
	}
	
	return arr;
}
var arr = [ 5,9,8,2,4]
var result = BubbleSort3(arr);



function BubbleSort4(arr){
	var i, j, temp, flag = true;

	for(i = arr.length; i > 0 && flag; i--){
		flag = false;
		for(j = 0; j < i - 1; j++){
			if(arr[j] > arr[j+1]){
				temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
				flag = true;
			}
			console.log(i+':'+j);
			console.log(arr);
			console.log(flag)
		}
		console.log(i);
		console.log(arr);
		console.log(flag)
		console.log('-------------')
	}
	
	return arr;
}
var arr = [ 2,1,3,5,6,7,8,9]
var result = BubbleSort4(arr);