HTML Academy
Bubble Sort, Mastery Challenge 1
Bubble sort2/4
Back to the list of challenges
  • 1. Bubble Sort, Mastery Challenge 1
  • 2. Bubble Sort, Mastery Challenge 2
  • 3. Bubble Sort, Mastery Challenge 3
  • 4. Bubble Sort, Mastery Challenge 4
  • Sign up
  • Log in

Register to take up challenges

Registration will only take a minute and let you save your study progress. You can register with your email and password or login via social networks.

or
Log in and continue
  • index.html
  • style.css
  • script.js
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Algorithms: Bubble Sort</title> <link rel="stylesheet" href="setting.css"> </head> <body> <div class="plot-container plot-regular"></div> <script> function draw(arr, swap1, swap2) { const container = document.querySelector(".plot-container"); const row = document.createElement("div"); row.className = "row"; for (let i = 0; i < arr.length; i++) { const num = arr[i]; const div = document.createElement("div"); div.classList.add("dot", "dot-" + num); div.textContent = num; if ((swap1 === i) || (swap2 === i)) { div.classList.add("dot-swapped"); } row.appendChild(div); } container.appendChild(row); } </script> <script src="script.js"></script> </body> </html>
CSS
JavaScript
let arr = [6, 3, 8, 4, 5]; draw(arr); bubbleSort(arr); function bubbleSort(arr) { let len = arr.length - 1; for (let i = 0; i < len; i++) { if (arr[j] > arr[j + 1]) { let temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; draw(arr, j, j + 1); } } }

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Click inside the mini browser to put the focus in this window.

100%
Console
let arr = [6, 3, 8, 4, 5]; draw(arr); bubbleSort(arr); function bubbleSort(arr) { let len = arr.length - 1; for (let i = 0; i < len; i++) { for (let j = 0; j < len - i; j++) { if (arr[j] > arr[j + 1]) { let temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; draw(arr, j, j + 1); } } } }

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2025

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.