fork download
  1. // your code goes here
  2.  
  3. function bubbleSort(arr, n) {
  4. for(let i=0;i<n-1;i++) {
  5. for(let j=0;j<=n-i-2;j++) {
  6. if(arr[j]>arr[j+1]) {
  7. let tmp = arr[j];
  8. arr[j] = arr[j+1];
  9. arr[j+1] = tmp;
  10. // [arr[j], arr[j+1]] = [arr[j+1], arr[j]];
  11. }
  12. }
  13. }
  14. return arr;
  15. }
  16.  
  17. console.log(bubbleSort([4, 1, 3, 5, 2], 5))
Success #stdin #stdout 0.03s 16480KB
stdin
Standard input is empty
stdout
1,2,3,4,5