Thought: Starting with the first element, comparing 22 adjacent elements in an array, placing the element with the smaller value in front, the larger value of the element in the back, a round comparison, a maximum number of sink becoming the last element in the array, and some smaller numbers floating in a position like bubbles. n number, after the n-1 round comparison completed sorting.
var arr=[5,0,-56,900,12];
VAR flag=false;//flag bit, used for control program comparison number, if ordered, this flag can reduce the number of comparisons
for (Var i=0;i<arr.length-1;i++) {//Large sort count (arr.length-1)
Document.writeln ("Loop <br/>");//used to view the number of comparisons
for (Var j=0;j<arr.length-1-i;j++) {//small sort
if (Arr[j]>arr[j+1]) {
Exchange
var temp=arr[j];
ARR[J]=ARR[J+1];
Arr[j+1]=temp;
flag=true;//swapped.
}
}
if (flag) {
Flag=false;
}else{
Break;//was already orderly, and withdrew
}
}
Iterating through the sorted array of outputs
for (Var i=0;i<arr.length;i++) {
Document.writeln (arr[i]+ " ");
}
JavaScript for bubbling sorting