0
0
DSA Javascriptprogramming~20 mins

Bubble Sort Algorithm in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bubble Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Bubble Sort code?
Consider the following JavaScript code that sorts an array using Bubble Sort. What will be the printed output after running this code?
DSA Javascript
const arr = [5, 3, 8, 4, 2];
for (let i = 0; i < arr.length - 1; i++) {
  for (let j = 0; j < arr.length - 1 - i; j++) {
    if (arr[j] > arr[j + 1]) {
      const temp = arr[j];
      arr[j] = arr[j + 1];
      arr[j + 1] = temp;
    }
  }
}
console.log(arr.join(' -> ') + ' -> null');
A5 -> 3 -> 8 -> 4 -> 2 -> null
B2 -> 3 -> 4 -> 5 -> 8 -> null
C8 -> 5 -> 4 -> 3 -> 2 -> null
D2 -> 4 -> 3 -> 5 -> 8 -> null
Attempts:
2 left
💡 Hint
Remember Bubble Sort swaps adjacent elements if they are in the wrong order, pushing the largest to the end each pass.
🧠 Conceptual
intermediate
1:30remaining
How many total swaps does Bubble Sort perform on this array?
Given the array [4, 2, 7, 3], how many swaps will Bubble Sort perform to sort it in ascending order?
A6
B4
C3
D5
Attempts:
2 left
💡 Hint
Count each time two adjacent elements are swapped during the sorting process.
Predict Output
advanced
2:00remaining
What error does this Bubble Sort code produce?
Analyze the following Bubble Sort code snippet. What error will it raise when run?
DSA Javascript
const arr = [3, 1, 4];
for (let i = 0; i <= arr.length; i++) {
  for (let j = 0; j < arr.length - 1 - i; j++) {
    if (arr[j] > arr[j + 1]) {
      [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
    }
  }
}
console.log(arr.join(' -> ') + ' -> null');
ANo error, outputs: 1 -> 3 -> 4 -> null
BRangeError: Maximum call stack size exceeded
CTypeError: Cannot read property 'length' of undefined
Dundefined -> undefined -> undefined -> null
Attempts:
2 left
💡 Hint
Check the loop conditions carefully for off-by-one errors causing undefined access.
🔧 Debug
advanced
2:00remaining
Identify the bug in this Bubble Sort implementation
This Bubble Sort code is intended to sort an array ascending but produces incorrect output. What is the bug?
DSA Javascript
const arr = [6, 2, 9, 1];
for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr.length - 1; j++) {
    if (arr[j] < arr[j + 1]) {
      const temp = arr[j];
      arr[j] = arr[j + 1];
      arr[j + 1] = temp;
    }
  }
}
console.log(arr.join(' -> ') + ' -> null');
AThe outer loop should run from arr.length - 1 down to 0
BThe inner loop should run fewer times each pass to avoid unnecessary comparisons
CThe temporary variable is not used correctly causing wrong swaps
DThe comparison operator should be '>' to sort ascending, not '<'
Attempts:
2 left
💡 Hint
Check the comparison direction to see if it sorts ascending or descending.
🚀 Application
expert
2:00remaining
How many passes does Bubble Sort need to sort this array?
Given the array [1, 2, 3, 4, 5], how many passes (outer loop iterations) will Bubble Sort perform before it stops if optimized to stop early when no swaps occur?
DSA Javascript
const arr = [1, 2, 3, 4, 5];
let n = arr.length;
for (let i = 0; i < n; i++) {
  let swapped = false;
  for (let j = 0; j < n - 1 - i; j++) {
    if (arr[j] > arr[j + 1]) {
      [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
      swapped = true;
    }
  }
  if (!swapped) break;
}
console.log(arr.join(' -> ') + ' -> null');
A1
B4
C5
D0
Attempts:
2 left
💡 Hint
If the array is already sorted, the algorithm stops after the first pass with no swaps.