constorder=(array,index)=>{if(array[index]>array[index+1]){// These two elements are unordered, so swap them.[array[index],array[index+1]]=[array[index+1],array[index]];// Note that a swap occurred.return1;}else{// Note that no swap occurred here.return0;}}constbubbleSort=(array)=>{for(;;){letswapped=0;for(letindex=0;index<array.length-1;index++){// Attempt to order each pair in turn, noting if a swap occurred.swapped|=order(array,index);}if(swapped===0){// If no swaps occurred the array is totally ordered.returnarray;}}}
A little abstraction can go a long way.
Thanks a lot for sharing your code, I can see a lot of improvements needed on my code and modified my version too ππΎπ