Optimized Bubble Sort

Run Settings
LanguageJavaScript
Language Version
Run Command
function bSort(arr){ let swapCount=1; while(swapCount>0){ swapCount=0; for(let j=1; j<arr.length; j++){ if(arr[j-1]>arr[j]){ //Swapping elements arr[j-1]+=arr[j]; arr[j]=arr[j-1]-arr[j]; arr[j-1]=arr[j-1]-arr[j]; swapCount++; } } } return arr } bSort([1,5,4,2,3]); console.log();
Editor Settings
Theme
Key bindings
Full width
Lines