Bubble sort: Arranges the elements in an array in order from big to small or from small to large.
var array=[9,8,7,6,5,4,3,2,1];
The first round comparison: 8,7,6,5,4,3,2,1,9 exchanged 8 times i=0 j=array.length-1-i
Second round comparison: 7,6,5,4,3,2,1,8,9 exchanged 7 times I=1 J=array.length-1-i
Third round comparison: 6,5,4,3,2,1,7,8,9 exchanged 6 times i=2 J=array.length-1-i
The fourth round comparison: 5,4,3,2,1,6,7,8,9 exchanged 5 times i=3 J=array.length-1-i
The fifth round comparison: 4,3,2,1,5,6,7,8,9 exchanged 4 times i=4 j=array.length-1-i
The sixth round comparison: 3,2,1,4,5,6,7,8,9 exchanged 3 times i=5 j=array.length-1-i
The seventh round comparison: 2,1,3,4,5,6,7,8,9 exchanged 2 times i=6 j=array.length-1-i
The eighth round comparison: 1,2,3,4,5,6,7,8,9 exchanged 1 times i=7 j=array.length-1-i
Code implementation:
var temp;
var array=[9,8,7,6,5,4,3,2,1];
Outer loop control number of rounds for
(var i=0;i<array.length-1;i++) {
//inner loop control comparison for
(Var j=0;j<array.length-1-i;j++) {
if (array[j]>array[j+1]) {
//Exchange two variable
temp=array[j];
ARRAY[J]=ARRAY[J+1];
Array[j+1]=temp
}
}} Console.log (array);
Code optimization:
var temp,bool,m=0;
var array=[9,8,7,6,5,4,3,2,1];
for (Var i=0;i<array.length-1;i++) {
//Toggle principle Switch
bool = true;
for (Var j=0;j<array.length-1-i;j++) {
if (array[j]>array[j+1]) {
//Exchange two variables
temp=array[j];
ARRAY[J]=ARRAY[J+1];
Array[j+1]=temp;
bool=false;//the switch off
}
//If the inner loop if is not executed (switch off, execute the following statement);
if (bool) {break
;
}
m++;
}
Console.log (array+ ", compare" +m+ ");
Note: Compare the number of wheels to the best 0 rounds, the worst for 8 rounds
Let's see a bubble sort algorithm.
//javascript bubble sort, added directly to the base type's prototype//here, add a method to the underlying type prototype with the code on the pristine JavaScript language,//because the Array,stri Ng They are constructors themselves, they also create objects through the new constructor line, so array.prototype,string.prototype all point to Function.prototype// Array.method, the first access to the array itself function object without method methods, and then Array.prototype still not, then Function.prototype found Function.prototype.method = function (name, func) {if (!this.prototype[name]) {///It is best to determine whether this method is in the prototype, if it is not added this.prototype[name] = f
Unc
return to this;
};
Array.method (' bubble ', function () {//bubble algorithm in total cycle the length of the array, that is, Len times, each time will be the smallest put the last var len = this.length;
var i = 0, j = 0, tmp = 0; for (i=0 i < len; i++) {for (j = 0; (j + 1) < len-i;
J + +) {Console.log () if (This[j] > this[j+1]) {tmp = This[j];
THIS[J] = this[j+1];
THIS[J+1] = tmp;
}
};
};
return this;
});
Alert ([21,32,1,31,22,45,68,37,].bubble ());
Looking at another front engineer, the west wind of the Thin horse code, in the first layer for loop join initializes an Exchange Exchange token to false, and when there is an exchange occurs, it becomes true, adding a judgment after the second-level for loop, or false, that is, there is no exchange from after going to the Prove that the size is in the correct order, you can break to jump out of the outer for loop.
The array to be sorted
var list = Array (n, K, MB, k,);
Array length
var n = list.length;
The temporary variable of the Exchange order
var tmp;//
//Exchange flag
var Exchange;
Up to do n-1 trip sort for
(var time = 0; time <n-1; time + +) {
exchange = false;
for (var i = n-1; i> time; i–-) {
if (List[i] <list[i-1]) {
exchange = true;
TMP = list[i-1];
LIST[I-1] = List[i];
List[i] = tmp;
}
}
If this order does not occur in Exchange, advance termination algorithm
if (!exchange) {break
;
}
}
Alert (' array sort: ' + list + ', n altogether row ' + Time + ' trip ');
has also collected a netizen's algorithm before, also quite good, everybody looks down
function Bubblesort (array) {
var length = Array.Length;
var temp;
var Issort=false;
for (var i = 1; i < length; i++) {
issort = false;
for (var j = 0; J < Length-i; J +) {
if (Array[j] > Array[j+1]) {
//exchange
temp = array[j];
ARRAY[J] = array[j+1];
ARRAY[J+1] = temp;
Issort = true;
}
}
if (!issort) break; If no exchange occurs, exit the Loop
}
var array =[10,-3,5,34,-34,5,0,9];
Bubblesort (array);
for (var i=0;i< array.length;i++) {
document.write (array[i]+ "");
Well, let's wrap this up for you today, and hopefully it will help to learn JavaScript bubble sort for our little friends.