目录

所谓冒泡排序就是将一个数组中的元素按照你代码逻辑是从小到大,还是从大到小来排列,两两之之间依此进行排序比较 ,大的放在后面,在和后面的继续比较
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- head>
-
- <body>
- <script>
-
- script>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- head>
-
- <body>
- <script>
-
-
- var sex = [9, 55, 2, 12, 22, 5]
-
-
-
- script>
- body>
-
- html>
因为是两两之间进行排序,所以如果循环的话,那么循环次数是不是应该就是数组的长度-1,
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- head>
-
- <body>
- <script>
-
-
- var sex = [9, 55, 2, 12, 22, 5]
-
-
- for (var i = 0; i < sex.length - 1; i++) {
-
- }
- }
- script>
- body>
-
- html>
这里的i要从0或1开始都可以,他控制的只是循环排序次数,里面的for循环必须是0开始,因为数组下标是0开始
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- head>
-
- <body>
- <script>
-
-
- var sex = [9, 55, 2, 12, 22, 5]
-
-
- for (var i = 0; i < sex.length - 1; i++) {
- for (var j = 0; j < sex.length - 1; j++) {
-
-
-
- }
- }
- script>
- body>
-
- html>
内层for循环的话也是一样的判断条件。只不过这里的话可以优化一下代码执行次数
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- head>
-
- <body>
- <script>
-
-
- var sex = [9, 55, 2, 12, 22, 5]
-
-
- for (var i = 0; i < sex.length - 1; i++) {
- for (var j = 0; j < sex.length - 1-i; j++) {
-
- }
- }console.log(sex);
- script>
- body>
-
- html>
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- head>
-
- <body>
- <script>
-
-
- var sex = [9, 55, 2, 12, 22, 5]
-
-
- for (var i = 0; i < sex.length - 1; i++) {
- for (var j = 0; j < sex.length - 1; j++) {
- if (sex[j] > sex[j + 1]) {
-
-
- }
- }
- }
- script>
- body>
-
- html>
这里我们添加一个判断条件,如果说第一个数大于第二个数,那么我们就把第一个数与第二个数进行一个交换,这里的话就要用到之前说的交换变量
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- head>
-
- <body>
- <script>
-
-
- var sex = [9, 55, 2, 12, 22, 5]
-
-
- for (var i = 0; i < sex.length - 1; i++) {
- for (var j = 0; j < sex.length - 1; j++) {
- if (sex[j] > sex[j + 1]) {
- var temp = sex[j + 1]
- sex[j + 1] = sex[j]
- sex[j] = temp
-
- }
- }
- }
- script>
- body>
-
- html>
这样的话我们就实现了效果,接下面我们输出一下看看
- html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Documenttitle>
- head>
-
- <body>
- <script>
-
-
- var sex = [9, 55, 2, 12, 22, 5]
-
-
- for (var i = 0; i < sex.length - 1; i++) {
- for (var j = 0; j < sex.length - 1; j++) {
- if (sex[j] > sex[j + 1]) {
- var temp = sex[j + 1]
- sex[j + 1] = sex[j]
- sex[j] = temp
-
- }
- }
- }console.log(sex);
- script>
- body>
-
- html>
