<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let arr = []
let num = Number(prompt())
while (num != 99) {
arr.push(num)
num = Number(prompt())
}//读取待排序元素
console.log(arr)
for (let i = 0; i < arr.length - 1; i++) {
let finish = true
for (let j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
let tmp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = tmp
finish = false
}
}
if (finish == true)
break
}
alert(`有序序列为${arr}`)
</script>
</body>
</html>
JavaScript--冒泡排序算法
最新推荐文章于 2024-07-23 11:32:13 发布