天天看点

JS语法基础_数组_冒泡排序

<!doctype html>

<html >

<head>

<meta charset="UTF-8" />

<title>demo01</title>

</head>

<body>

<input  type="button" value="测试" οnclick="show()" />

</body>

</html>

<script type="text/javascript" >

//2,4,7,5,3,6,1

function show(){

var arr=[2,4,7,5,3,6,1];

for(var i=0;i<arr.length-1;i++){

for(var j=0;j<arr.length-1-i;j++){

//1、比较相邻的两个数;大的在后,小的在前

if(arr[j] > arr[j+1] ){

var temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

}

console.log(arr);

}

</script>

继续阅读