Advertisement
aleffelixf

Bubble Sort in Javascript

Sep 10th, 2021
1,381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2.  
  3. const bubbleSort = (arr) => {
  4.     for (let i = 0; i < arr.length; i++) {
  5.         for (let j = 0; j < arr.length; j++) {
  6.             if (arr[j] > arr[j + 1]) {
  7.                 let temp = arr[j];
  8.                 arr[j] = arr[j + 1];
  9.                 arr[j + 1] = temp;
  10.             }
  11.         }
  12.     }
  13.  
  14.     return arr;
  15. };
  16.  
  17. console.log(bubbleSort([4, 1, 5, 3, 34, 89, 11]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement