Home > Web Front-end > JS Tutorial > body text

How to implement bubble sort

巴扎黑
Release: 2017-06-26 15:15:41
Original
1225 people have browsed it
 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>Title</title> 6 </head> 7  8 <body> 9 <script>10     var arr = [3,2,4,1,5];11 12     /*13     * 每次循环比较,取出当前的值和他的下一位进行大小的比较,如果当前值比下一个要大(小),交换位置,每次循环确定一个最大(最小)数14     *15     * 因为每次比较的数列中,最后一个没有下一位,所以最后一次没有必要再参与比较,所以每次循环比较的次数是要比较的数列元素个数-1次16     * */17     for (var i=0; i<arr.length-1; i++) {18 19         // 获取当前位的值20         var a = arr[i];21         // 获取下一位的值22         var b = arr[i+1];23 24         // 这里我们以小值在后,如果a小于b交换位置25         if (a < b) {26             arr[i] = b;27             arr[i+1] = a;28         }29 30     }31 32     // 进过上面的一轮循环,就确定这个数列中需要比较的值中最小的值33     console.log(arr);34 35 </script>36 </body>37 </html>
Copy after login
 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>Title</title> 6 </head> 7  8 <body> 9 <script>10 //    var arr = [3,2,4,1,5];11 12     var arr = [];13     for (var i=0; i<30000; i++) {14         arr.push(i);15     }16     arr.sort(function () {17         return Math.random() - 0.5;18     });19 20     /*21     * 每一轮的比较确定一个值,整个比较过程需要比较的次数是 长度-1,以为最后一轮的值,只有一个了,没有比较在比较了22     * */23 24     /*25     * 统计循环的总次数26     * */27     var n = 0;28 29     console.time(&#39;a&#39;);30     for ( var j=0; j<arr.length-1; j++ ) {31 32         for (var i=0; i<arr.length-1; i++) {33             var a = arr[i];34             var b = arr[i+1];35             if (a < b) {36                 arr[i] = b;37                 arr[i+1] = a;38             }39 40             n++;41         }42 43     }44     console.timeEnd(&#39;a&#39;);45 46     console.log(n);47     console.log(arr);48 49 </script>50 </body>51 </html>
Copy after login
 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>Title</title> 6 </head> 7  8 <body> 9 <script>10     //var arr = [3,2,4,1,5];11 12     var arr = [];13     for (var i=0; i<30000; i++) {14         arr.push(i);15     }16     arr.sort(function () {17         return Math.random() - 0.5;18     });19 20     var n = 0;21 22     console.time(&#39;a&#39;);23     for ( var j=0; j<arr.length-1; j++ ) {24 25         /*26         * 随着大的循环的次数的增加,对应的小的循环就应该减少,减少j次27         * */28         for (var i=0; i<arr.length-1-j; i++) {29             var a = arr[i];30             var b = arr[i+1];31             if (a < b) {32                 arr[i] = b;33                 arr[i+1] = a;34             }35 36             n++;37 38         }39 40     }41     console.timeEnd(&#39;a&#39;);42 43     console.log(n);44     console.log(arr);45 46 </script>47 </body>48 </html>
Copy after login





Title


< script>
// var arr = [3,2,4,1,5];
// var arr = [5,4,3,1,2];

var arr = [];
for (var i=0; i<30000; i++) {
arr.unshift(i);
}
arr[29999] = 1;
arr[ 29998] = 0;

// arr.sort(function () {
// return Math.random() - 0.5;
// });

var n = 0;

console.time('a');
for ( var j=0; j

/*
* As the number of large loops increases, the corresponding small loops should be reduced, reducing by j times
* */

/*
* Each time a smaller loop is made , set the flag to true, indicating that it has been sorted, assuming that the sorting is ok
  * */
  var flag = true;

for (var i=0; i var a = arr[i];
var b = arr[i+1];
if (a < * If the if condition is left during the comparison process, it means that the comparison is not completed and needs to be compared next time. Otherwise, the exchange of positions has not occurred, indicating that the data has been sorted
                                                             /*
            * As long as the if is left, indicating that there is an exchange, set the flag to false
                                                                                                                                                                   arr[i+ 1] = a;

}


n++;

}

/*
, it means that the above loop occurs without exchange, indicating that the sorting is ok. If it is false, it means that there has been exchange.

##}

console.timeEnd('a');

console.log(n);

console.log(arr);





The above is the detailed content of How to implement bubble sort. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template