Bubble sort example, bidirectional bubble sort with slightly improved visualization of bidirectional bubble sort.
The code is very simple, I don't know if there are any unknown bugs.
God, please don't complain
Bubble sorting example
var ls=[ 98,13,6,25,38,36,30,44,38,80,61,28,47,34,95,18,85,58,89,85,42,61,74,35,13,14,80,7,10,44,10,47,13,11,52,25,24,48,34,12,88,80,33,80,45,64,52,79,77 ]; for(var i=0;i<ls.length;i++){ for(var j=i+1;j<ls.length;j++){ if(ls[i]>ls[j]){ ls[i]=ls[i]+ls[j]; ls[j]=ls[i]-ls[j]; ls[i]=ls[i]-ls[j]; } } }Two-way bubble sorting example
var ls=[ 6,13,98,25,38,36,30,44,38,80,61,28,47,34,95,18,85,58,89,85,42,61,74,35,13,14,80,7,10,44,10,47,13,11,52,25,24,48,34,12,88,80,33,80,45,64,52,79,77 ]; for(var i=0;i<ls.length;i++){ for(var j=i+1;j<ls.length-i;j++){ if(ls[lent-1-i]<ls[lent-j]){ ls[lent-1-i]=ls[lent-1-i]+ls[lent-j]; ls[lent-j]=ls[lent-1-i]-ls[lent-j]; ls[lent-1-i]=ls[lent-1-i]-ls[lent-j]; ls[lent-1-i]=ls[lent-1-i]-ls[lent-j]; }//The following comparison if(ls[i]>ls[j]){ ls[i]=ls[i]+ls[j]; ls[j]=ls[i]-ls[j]; ls[i]=ls[i]-ls[j]; }//The previous comparison} }Example of slightly improved bidirectional bubble sort
var ls=[ 98,13,6,25,38,36,30,44,38,80,61,28,47,34,95,18,85,58,89,85,42,61,74,35,13,14,80,7,10,44,10,47,13,11,52,25,24,48,34,12,88,80,33,80,45,64,52,79,77 ]; var lent=ls.length;for(var i=0;i<ls.length;i++){ for(var j=i*2;j<ls.length-2*i;j++){ if(ls[i*2]>ls[j+1]){ ls[i*2]=ls[i*2]+ls[j+1]; ls[j+1]=ls[i*2]-ls[j+1]; ls[i*2]=ls[i*2]-ls[j+1]; ls[i*2]=ls[i*2]-ls[j+1]; }// Keep the first number in the inner layer as the minimum loop if(ls[lent-i*2-1]<ls[lent-j-1]){ ls[lent-i*2-1]=ls[lent-i*2-1]+ls[lent-j-1]; ls[lent-j-1]=ls[lent-i*2-1]-ls[lent-j-1]; ls[lent-i*2-1]=ls[lent-j-1]; }//// Keep the penultimate number of the inner layer to the maximum cycle if(ls[lent-2-i*2]<ls[lent-j-1]){ ls[lent-2-i*2]=ls[lent-2-i*2]+ls[lent-j-1]; ls[lent-j-1]=ls[lent-2-i*2]-ls[lent-j-1]; ls[lent-2-i*2]=ls[lent-j-1]; ls[lent-2-i*2]=ls[lent-j-1]; ls[lent-2-i*2]=ls[lent-j-1]; }//Reciprocate the last if(ls[i*2+1]>ls[j+1]){ ls[i*2+1]=ls[i*2+1]+ls[j+1]; ls[j+1]=ls[i*2+1]-ls[j+1]; ls[i*2+1]=ls[i*2+1]-ls[j+1]; }//Next}}