1. What is Bubble Sort If an unordered sequence of numbers needs to be sorted from small to large, then when two elements are compared, it can be achieved by exchanging them, and the element on the left must be smaller than the element on the right. If an unordered sequence of numbers needs to be sorted from largest to smallest, then when two elements are compared, this can be achieved by exchanging them, so that the element on the left is larger than the element on the right. Just like the bubbles in a carbonated drink, bubbling from the bottom all the way to the top. 2. Give an exampleIf there is a set of numbers 2,4,7,5,3,6,1 Round 1: j (inner loop) loops 6 times. The work done by the inner loop is: compare two adjacent numbers, the larger one will be placed at the end and the smaller one at the front. The outer loop controls the number of times in one loop, and the inner loop makes judgments
Round 2: j (inner loop) loops 5 times
Round 3: j (inner loop) loops 4 times
Round 4: j (inner loop) loops 3 times
Round 5:
Round 6:
<script type="text/javascript" > // Example 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. Compare two adjacent numbers; the larger one is at the back, the smaller one is at the front if (arr[j] > arr[j+1] ) { var temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } console.log(arr); } // Example 2: <body> <input type="text" id="test"> <button type="button" onclick="show()">Press me</button> <input type="text" id="sc"> </body> function show() { let oT=document.getElementById("test").value; let sc = document.getElementById("sc"); // console.log(sc); // console.log(oT); let arr = oT.split(""); console.log(arr.length); for (var i = 0; i < arr.length - 1; i++) { for (var j = 0; j < arr.length - 1 - i; j++) { //1. Compare two adjacent numbers; the larger one is at the back, the smaller one is at the front if (arr[j] > arr[j + 1]) { var temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } // console.log(arr); sc.value=arr; } </script> SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: HTML+css to create a simple progress bar
>>: Javascript tree menu (11 items)
Glass Windows What we are going to achieve today ...
Preface Recently, I encountered a requirement at ...
A problem occurred when configuring a cluster. Or...
An n-digit verification code consisting of number...
Preface We often say that node is not a new progr...
Table of contents 0. What is Webpack 1. Use of We...
Solution to Ubuntu dual system stuck when startin...
In the past two days, I have been very troubled t...
Table of contents 1 Create configuration and data...
Table of contents 1. Bootstrap5 breakpoints 1.1 M...
Report an error The Apache\Nginx service started ...
1. Problem Description root@mysqldb 22:12: [xucl]...
Table of contents 1. What is a design pattern? 2....
This article uses an example to describe how to u...
As the application of centos on the server side b...