Bubble sorting: It is to arrange the elements in an array in order from large to small or from small to large.
var array=[9,8,7,6,5,4,3,2,1];
First round comparison: 8,7,6,5,4,3,2,1,9 swapped 8 times i=0 j=array.length-1-i
Second round comparison: 7,6,5,4,3,2,1,8,9 swapped 7 times i=1 j=array.length-1-i
The third round comparison: 6,5,4,3,2,1,7,8,9 swap 6 times i=2 j=array.length-1-i
4th round comparison: 5,4,3,2,1,6,7,8,9 swapped 5 times i=3 j=array.length-1-i
Fifth round comparison: 4,3,2,1,5,6,7,8,9 swapped 4 times i=4 j=array.length-1-i
Sixth round comparison: 3,2,1,4,5,6,7,8,9 swapped 3 times i=5 j=array.length-1-i
7th round comparison: 2,1,3,4,5,6,7,8,9 swapped 2 times i=6 j=array.length-1-i
8th round comparison: 1,2,3,4,5,6,7,8,9 swapped 1 time i=7 j=array.length-1-i
Code implementation:
var temp;var array=[9,8,7,6,5,4,3,2,1];//Number of external loop control rounds for(var i=0;i<array.length-1;i++){//Number of internal loop control comparison times for(var j=0;j<array.length-1-i;j++){ if(array[j]>array[j+1]){ //Swap two variables 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++){ //Switch bool in the opening and closing principle bool = true; for(var j=0;j<array.length-1-i;j++){ if(array[j]>array[j+1]){ //Swap two variables temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; bool=false;//Switch the switch} } //If the if in the inner loop is not executed (the switch is closed, execute the following statement); if(bool){ break; } m++;}console.log(array+", compare "+m+"wheel");Note: The best case of comparison rounds is 0 rounds, and the worst case is 8 rounds.
Let's look at a bubble sorting algorithm
//javascript bubble sorting, and directly add it to the prototype of the basic type// Here we use a javascript language essence code to add methods to the basic type prototype. // Because Array and String themselves are constructors, they create objects through the new constructor line, so Array.prototype and String.prototype all point to Function.prototype // When Array.method, first access Array's own function object, there is no method method, then Array.prototype still does not exist, and then Function.prototype finds Function.prototype.method = function (name, func) { if(!this.prototype[name]) { //It is best to judge whether there is this method in the prototype first. If this.prototype[name] = func; } return this; }; Array.method('bubble',function(){ // The bubble algorithm loops the length of the array in total, that is, len times, and the smallest one is put at 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());I looked at the code of another front-end engineer, Xifeng Shouma. In the first layer of for loop, an exchange exchange flag is false. When there is an exchange, it becomes true. A judgment is added after the second layer of for loop ends. If it is false, that is, there is no exchange from front to back, and it proves that the size order is correct, you can break to jump out of the outer for loop.
//Arrays that need to be sorted var list = Array(23, 45, 18, 37, 92, 13, 24);//Array length var n = list.length;//Temporary variables in exchange order var tmp;////Exchange flag var exchange;//At most n-1 ordering 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]; list[i] = tmp; } } //If there is no exchange in this order, the algorithm is terminated early if (!exchange) { break; }}alert('Array sorted is: ' + list + ',n has been arranged in total ' + time + 'walk');I have collected a netizen's algorithm before, which is quite good. Please take a look at it.
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]) { //Swap 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]+ " "); }Okay, let’s summarize these for you today. I hope it will be helpful for friends to learn JavaScript bubble sorting.