Task: Set a random number of 5 numbers, using the JS bubble sorting method to order from small to large.
such as: [5 3 4 1 2] The output is [1 2 3 4 5].
----------------------------------------------------------------------------------
"Explanation: What is the bubble sorting method"
The so-called bubble sorting method, is a set of numbers from the big to small or from the smallest to the big sort of an algorithm. The exact method is that the adjacent value 22 is exchanged. Starting with the first value, if the next two numbers are arranged in a different order than our expectations, the position of the two digits is swapped (swapped), and if it is consistent with our expectations, it is not exchanged. Repeat the process until there is no value at the end that needs to be exchanged, then the sort is complete. Generally, if there are N number need to sort, then need to do (N-1) bubbling, we take from small to large sort as an example to see, the specific situation as shown:
----------------------------------------------------------------------------------
"Analysis" How to sort [5, 3, 4, 2, 1] with the bubbling sort method?
(Important: When comparing sizes, the first compares the 1th and 2nd numbers, the second compares the 2nd and 3rd numbers, and so on.) )
Sequence: [5, 3, 4, 2, 1]
The first cycle, 5>3, the two Exchange positions: 3,5,4,2,1
Then, 5>4, the two Exchange positions: 3,4,5,2,1
Then, 5>2, the two Exchange positions: 3,4,2,5,1
Then, 5>1, the two Exchange positions: 3,4,2,1,5
At this point, the sequence becomes [3,4,2,1,5], followed by a second large loop:
Second cycle, 3<4, the two do not exchange position: 3,4,2,1,5
Then, 4>2, the two Exchange positions: 3,2,4,1,5
Then, 4>1, the two Exchange positions: 3,2,1,4,5
At this point, the sequence becomes [3,2,1,4,5], followed by a third large loop:
The third cycle, 3>2, is the exchange of the two positions: 2,3,1,4,5
Then, 3>1, the two Exchange positions: 2,1,3,4,5
At this point, the sequence becomes [2,1,3,4,5], followed by a fourth large loop:
Fourth cycle, 2>1, the two Exchange positions: 1,2,3,4,5
After 4 cycles of sorting, it is successfully sorted by the small to the general number.
Therefore, a total of 4 cycles are required (i = 0; I < series length; i++)
For each sort, compare the number of J and j+1 (j = 0; J < sequence length-I; j + +)
When number of J > number j+1, two digits are swapped.
"Transposition Mode":
When A>b, want to let A and B exchange position, how to do?
Set X as the intermediate over-zone.
Make x=b,//Put the value of B in X first
B=a,//Place the value of a in the position of B
A=x,//put the b value stored in X in the position of a
The value exchange of A and B is complete.
--------------------------------------------------------------------------
Program
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"><Html><Head><Title>new Document</Title></Head><Body><ScriptLanguage= "JavaScript"Type= "Text/javascript"><!--VarArray=[5,3,4,2,1];VarTemp=0;For(VarI=0; I<Array.Length; I++)//Set total number of loops {For(VarJ=0; J<Array.Length-I J++)//Set Each loop to be sorted several times {If(Array[j]>Array[j+1])//Compare the number of J and j+1, and change the position when j>j+1 {Temp=Array[j+1]; Array[j+1]=ARRAY[J]; Transposition Process Array[j]=Temp } }}For(Var I=0;i<array.length;i +++" ); Output sequence in current order}//--> </script></body></< Span style= "color: #800000;" >html>
js Bubble sort