思路:按照值小的元素上浮,值大的元素下沉的思路,代码如下
``
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>冒泡排序</title>
</head>
<body>
<script>
function bubbleSort(array){
for (let i=array.length;i>0;i--){
for (let j=0;j<i;j++){
if (array[j]>array[j+1]){
var temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
return array;
}
console.log(bubbleSort([10,8,3,2,2,4,9,5,4,3]));
</script>
</body>
</html>
//测试
var arr=[10,8,3,2,2,4,9,5,4,3];
console.log(bubbleSort([10,8,3,2,2,4,9,5,4,3]));
//22334458910