Quick actions

cmd+k|ctrl+k

Navigation

Languages

Bubble sorting

Snippet info

Language

JavaScript

Visibility

public

Author

riki.whyudi

Created

2022-12-30T03:07:44.386633Z

Updated

2022-12-30T03:07:44.386633Z

// Generate an array of random numbers
const data = [];
for (let i = 0; i < 10; i++) {
  data.push(Math.floor(Math.random() * 100));
}

console.log(data); // Output: [47, 92, 27, 53, 37, 31, 60, 26, 21, 58]

// Sort the array in ascending order
for (let i = 0; i < data.length; i++) {
  for (let j = 0; j < data.length; j++) {
    if (data[j] > data[j + 1]) {
      // Swap the elements
      const temp = data[j];
      data[j] = data[j + 1];
      data[j + 1] = temp;
    }
  }
}

console.log(data); // Output: [21, 26, 27, 31, 37, 47, 53, 58, 60, 92]
INFO