📖 Scenario: You have a list of numbers that you want to arrange from smallest to largest, like sorting books by height on a shelf.
🎯 Goal: Build a program that sorts a list of numbers using the Bubble Sort method, which compares pairs of numbers and swaps them if they are in the wrong order.
📋 What You'll Learn
Create an array called numbers with the exact values: [5, 3, 8, 4, 2]
Create a variable called n that stores the length of the numbers array
Use nested for loops with variables i and j to perform the Bubble Sort algorithm
Swap elements in the numbers array when the current element is greater than the next element
Print the sorted numbers array at the end
💡 Why This Matters
🌍 Real World
Sorting is used everywhere, like organizing contacts by name or arranging products by price in online stores.
💼 Career
Understanding sorting algorithms helps in software development, data analysis, and improving program efficiency.
Progress0 / 4 steps
1
Create the initial array
Create an array called numbers with these exact values: [5, 3, 8, 4, 2]
DSA Javascript
Hint
Use const numbers = [5, 3, 8, 4, 2]; to create the array.
2
Set up the length variable
Create a variable called n that stores the length of the numbers array
DSA Javascript
Hint
Use const n = numbers.length; to get the array length.
3
Implement the Bubble Sort loops
Use nested for loops with variables i and j to perform Bubble Sort on the numbers array. Swap elements when numbers[j] is greater than numbers[j + 1].
DSA Javascript
Hint
Use two for loops: outer loop with i from 0 to n-1, inner loop with j from 0 to n-1-i. Swap elements if numbers[j] is greater than numbers[j+1].
4
Print the sorted array
Print the numbers array after sorting using console.log(numbers)
DSA Javascript
Hint
Use console.log(numbers); to print the sorted array.
Practice
(1/5)
1. What is the main idea behind the Bubble Sort algorithm?
easy
A. Repeatedly swap adjacent elements if they are in the wrong order
B. Divide the array into halves and sort each half separately
C. Select the smallest element and place it at the beginning
D. Insert each element into its correct position in a sorted sublist
Solution
Step 1: Understand Bubble Sort mechanism
Bubble Sort compares adjacent elements and swaps them if they are out of order.
Step 2: Compare with other sorting methods
Other options describe different algorithms: selection sort (A), divide and conquer (B), insertion sort (D).
Final Answer:
Repeatedly swap adjacent elements if they are in the wrong order -> Option A
Quick Check:
Bubble Sort = swapping adjacent pairs [OK]
Hint: Bubble Sort swaps neighbors repeatedly to sort [OK]
Common Mistakes:
Confusing Bubble Sort with Selection Sort
Thinking it divides the array like Merge Sort
Assuming it inserts elements like Insertion Sort
2. Which of the following is the correct syntax to swap two elements arr[i] and arr[i+1] in JavaScript during Bubble Sort?
easy
A. arr[i] = arr[i+1]; arr[i+1] = arr[i];
B. arr[i], arr[i+1] = arr[i+1], arr[i];
C. swap(arr[i], arr[i+1]);
D. let temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp;
Solution
Step 1: Understand JavaScript swapping
JavaScript does not support tuple unpacking; use a temporary variable to swap.
Step 2: Analyze each option
let temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; uses a temp variable correctly. arr[i], arr[i+1] = arr[i+1], arr[i]; is Python syntax, invalid in JS. swap(arr[i], arr[i+1]); calls undefined function. arr[i] = arr[i+1]; arr[i+1] = arr[i]; overwrites values incorrectly.
Final Answer:
let temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; -> Option D
Quick Check:
Use temp variable to swap in JS [OK]
Hint: Use temp variable to swap in JavaScript [OK]
Common Mistakes:
Using Python-style tuple swap in JavaScript
Calling undefined swap function
Overwriting values without temp variable
3. What will be the output of the following JavaScript code snippet?
let arr = [4, 2, 3];
for(let i = 0; i < arr.length - 1; i++) {
if(arr[i] > arr[i+1]) {
let temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
console.log(arr);
medium
A. [2, 4, 3]
B. [2, 3, 4]
C. [4, 2, 3]
D. [3, 2, 4]
Solution
Step 1: Trace the single pass of the loop
Loop runs i=0 to 1. At i=0: 4 > 2, swap -> arr = [2,4,3]. At i=1: 4 > 3, swap -> arr = [2,3,4].
Step 2: Check loop range carefully
The loop runs only once over the array (i from 0 to length-2). So swaps happen at i=0 and i=1.
Final Answer:
[2, 4, 3] -> Option A
Quick Check:
One pass swaps neighbors to partially sort [OK]
Hint: Trace each swap carefully for one loop pass [OK]
Common Mistakes:
Assuming full sort after one pass
Ignoring second swap at i=1
Confusing array indices
4. Identify the error in this Bubble Sort code snippet and choose the correct fix:
B. Change outer loop to run from i = 1 to arr.length
C. Change inner loop condition to j < arr.length - i - 1
D. Add a check to skip swapping equal elements
Solution
Step 1: Analyze inner loop range
Inner loop runs till j < arr.length, so arr[j+1] can be undefined at last index causing error.
Step 2: Fix inner loop to avoid out-of-bound access
Changing condition to j < arr.length - i - 1 prevents accessing beyond array length and optimizes sorting.
Final Answer:
Change inner loop condition to j < arr.length - i - 1 -> Option C
Quick Check:
Inner loop must avoid out-of-bound index [OK]
Hint: Inner loop stops earlier each pass to avoid overflow [OK]
Common Mistakes:
Ignoring array boundary causing undefined access
Changing outer loop incorrectly
Removing inner loop breaks sorting
5. Given the array [5, 1, 4, 2, 8], how many total swaps will Bubble Sort perform to fully sort it in ascending order?
hard
A. 4
B. 6
C. 5
D. 7
Solution
Step 1: Perform Bubble Sort passes and count swaps
Pass 1 swaps: (5,1), (5,4), (5,2) -> 3 swaps (5 and 8 no swap).
Step 2: Continue passes counting swaps
Pass 2 swaps: (4,2) -> 1 swap. Pass 3 swaps: (4,3) none here but actually (2,3) no swap, but we need to check carefully. Actually, pass 3 swaps: (2,3) no swap, pass 4 no swaps. Total swaps: 3 + 1 + 2 = 6.
Step 3: Verify total swaps
Counting carefully, total swaps are 6, so 6.
Final Answer:
6 -> Option B
Quick Check:
Count swaps each pass carefully [OK]
Hint: Count swaps pass by pass to avoid missing any [OK]
Common Mistakes:
Assuming swaps equal to array length
Counting comparisons instead of swaps
Missing repeated swaps in later passes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.