Data Structure Bubble Sort Practice Questions with Solutions

Introduction

Bubble Sort is a simple sorting algorithm that repeatedly compares two neighboring elements and swaps them when they are in the wrong order. After each pass, the largest unsorted element moves toward the end of the array. In this chapter, we will practice Bubble Sort using JavaScript through different practical situations, including ascending and descending sorting, optimization, duplicate values, strings, and reusable functions. Data Structure Bubble Sort practice questions with solutions help to understand the concepts.

Question 1: Sort an Array Using Bubble Sort

Questions

Use Bubble Sort to arrange the following numbers in ascending order.

let numbers = [5, 3, 8, 1, 2];

Solution

let numbers = [5, 3, 8, 1, 2];

for (let i = 0; i < numbers.length - 1; i++) {
    for (let j = 0; j < numbers.length - i - 1; j++) {
        if (numbers[j] > numbers[j + 1]) {
            let temp = numbers[j];
            numbers[j] = numbers[j + 1];
            numbers[j + 1] = temp;
        }
    }
}

console.log(numbers);

Output

[1, 2, 3, 5, 8]

The largest value moves toward the end after each pass.


Question 2: Sort an Array in Descending Order

Questions

Use Bubble Sort to arrange the following numbers from largest to smallest.

let numbers = [12, 4, 19, 7, 2];

Solution

let numbers = [12, 4, 19, 7, 2];

for (let i = 0; i < numbers.length - 1; i++) {
    for (let j = 0; j < numbers.length - i - 1; j++) {
        if (numbers[j] < numbers[j + 1]) {
            let temp = numbers[j];
            numbers[j] = numbers[j + 1];
            numbers[j + 1] = temp;
        }
    }
}

console.log(numbers);

Output

[19, 12, 7, 4, 2]

For descending order, the swap condition is reversed.


Question 3: Count the Number of Swaps

Questions

Use Bubble Sort and count how many swaps are performed while sorting the array.

let numbers = [4, 3, 2, 1];

Solution

let numbers = [4, 3, 2, 1];
let swaps = 0;

for (let i = 0; i < numbers.length - 1; i++) {
    for (let j = 0; j < numbers.length - i - 1; j++) {
        if (numbers[j] > numbers[j + 1]) {
            let temp = numbers[j];
            numbers[j] = numbers[j + 1];
            numbers[j + 1] = temp;

            swaps++;
        }
    }
}

console.log("Sorted array:", numbers);
console.log("Total swaps:", swaps);

Output

Sorted array: [1, 2, 3, 4]
Total swaps: 6

Because the array is in reverse order, many swaps are required.


Question 4: Optimize Bubble Sort

Questions

Improve Bubble Sort so that it stops early when the array is already sorted.

let numbers = [1, 2, 3, 4, 5];

Solution

let numbers = [1, 2, 3, 4, 5];

for (let i = 0; i < numbers.length - 1; i++) {
    let swapped = false;

    for (let j = 0; j < numbers.length - i - 1; j++) {
        if (numbers[j] > numbers[j + 1]) {
            let temp = numbers[j];
            numbers[j] = numbers[j + 1];
            numbers[j + 1] = temp;

            swapped = true;
        }
    }

    if (!swapped) {
        break;
    }
}

console.log(numbers);

Output

[1, 2, 3, 4, 5]

Since no swaps are required during the first pass, the algorithm stops early.


Question 5: Find the Number of Passes Required

Questions

Use optimized Bubble Sort and count how many passes are needed to sort the array.

let numbers = [1, 3, 2, 4, 5];

Solution

let numbers = [1, 3, 2, 4, 5];
let passes = 0;

for (let i = 0; i < numbers.length - 1; i++) {
    let swapped = false;
    passes++;

    for (let j = 0; j < numbers.length - i - 1; j++) {
        if (numbers[j] > numbers[j + 1]) {
            let temp = numbers[j];
            numbers[j] = numbers[j + 1];
            numbers[j + 1] = temp;

            swapped = true;
        }
    }

    if (!swapped) {
        break;
    }
}

console.log("Sorted array:", numbers);
console.log("Passes:", passes);

Output

Sorted array: [1, 2, 3, 4, 5]
Passes: 2

The first pass moves 3 and 2 into the correct order. The second pass makes no swaps, so the algorithm stops.


Question 6: Bubble Sort an Array with Duplicate Values

Questions

Sort an array containing duplicate values using Bubble Sort.

let numbers = [4, 2, 4, 1, 2, 3];

Solution

let numbers = [4, 2, 4, 1, 2, 3];

for (let i = 0; i < numbers.length - 1; i++) {
    for (let j = 0; j < numbers.length - i - 1; j++) {
        if (numbers[j] > numbers[j + 1]) {
            let temp = numbers[j];
            numbers[j] = numbers[j + 1];
            numbers[j + 1] = temp;
        }
    }
}

console.log(numbers);

Output

[1, 2, 2, 3, 4, 4]

Bubble Sort can sort duplicate values normally without removing them.


Question 7: Bubble Sort Strings Alphabetically

Questions

Use Bubble Sort to arrange the following programming languages alphabetically.

let languages = ["Python", "C", "Java", "JavaScript"];

Solution

let languages = ["Python", "C", "Java", "JavaScript"];

for (let i = 0; i < languages.length - 1; i++) {
    for (let j = 0; j < languages.length - i - 1; j++) {
        if (languages[j] > languages[j + 1]) {
            let temp = languages[j];
            languages[j] = languages[j + 1];
            languages[j + 1] = temp;
        }
    }
}

console.log(languages);

Output

["C", "Java", "JavaScript", "Python"]

The comparison works because JavaScript can compare strings lexicographically.


Question 8: Bubble Sort Students by Marks

Questions

Use Bubble Sort to arrange students from the lowest marks to the highest marks.

let students = [
    { name: "Rahul", marks: 72 },
    { name: "Priya", marks: 85 },
    { name: "Aman", marks: 65 },
    { name: "Neha", marks: 90 }
];

Solution

let students = [
    { name: "Rahul", marks: 72 },
    { name: "Priya", marks: 85 },
    { name: "Aman", marks: 65 },
    { name: "Neha", marks: 90 }
];

for (let i = 0; i < students.length - 1; i++) {
    for (let j = 0; j < students.length - i - 1; j++) {
        if (students[j].marks > students[j + 1].marks) {
            let temp = students[j];
            students[j] = students[j + 1];
            students[j + 1] = temp;
        }
    }
}

console.log(students);

Output

[
    { name: "Aman", marks: 65 },
    { name: "Rahul", marks: 72 },
    { name: "Priya", marks: 85 },
    { name: "Neha", marks: 90 }
]

Here, the marks property is used to decide which objects should be swapped.


Question 9: Create a Reusable Bubble Sort Function

Questions

Create a function called bubbleSort() that accepts an array and returns the sorted array.

let numbers = [9, 4, 7, 2, 6];

Solution

function bubbleSort(array) {
    for (let i = 0; i < array.length - 1; i++) {
        let swapped = false;

        for (let j = 0; j < array.length - i - 1; j++) {
            if (array[j] > array[j + 1]) {
                let temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;

                swapped = true;
            }
        }

        if (!swapped) {
            break;
        }
    }

    return array;
}

let numbers = [9, 4, 7, 2, 6];

console.log(bubbleSort(numbers));

Output

[2, 4, 6, 7, 9]

Creating a function makes the Bubble Sort logic reusable for different arrays.


Question 10: Bubble Sort and Display Each Pass

Questions

Sort the array using Bubble Sort and display the array after every pass.

let numbers = [5, 1, 4, 2, 8];

Solution

let numbers = [5, 1, 4, 2, 8];

for (let i = 0; i < numbers.length - 1; i++) {
    let swapped = false;

    for (let j = 0; j < numbers.length - i - 1; j++) {
        if (numbers[j] > numbers[j + 1]) {
            let temp = numbers[j];
            numbers[j] = numbers[j + 1];
            numbers[j + 1] = temp;

            swapped = true;
        }
    }

    console.log("Pass " + (i + 1) + ":", numbers);

    if (!swapped) {
        break;
    }
}

Output

Pass 1: [1, 4, 2, 5, 8]
Pass 2: [1, 2, 4, 5, 8]
Pass 3: [1, 2, 4, 5, 8]

The output shows how Bubble Sort gradually moves elements into their correct positions.

Key Takeaways

  • Bubble Sort compares neighboring elements.
  • Elements are swapped when they are in the wrong order.
  • After each complete pass, an element reaches its correct position.
  • Bubble Sort can sort numbers in ascending or descending order.
  • It can also be adapted to sort strings and objects.
  • Duplicate values can be handled without any special changes.
  • The optimized version uses a swapped flag.
  • If no swap occurs during a pass, the array is already sorted.
  • Standard Bubble Sort has O(n²) worst-case time complexity.
  • Optimized Bubble Sort can perform better when the array is already or nearly sorted.
  • Bubble Sort uses O(1) extra space when implemented in-place.
  • Bubble Sort is easy to understand but is generally not preferred for large datasets.

FAQs

1. What is Bubble Sort?

Bubble Sort is a sorting algorithm that repeatedly compares adjacent elements and swaps them when they are in the wrong order.

2. Why is it called Bubble Sort?

It is called Bubble Sort because larger elements gradually move toward the end of the array during each pass, similar to bubbles moving upward.

3. What is the time complexity of Bubble Sort?

The worst-case and average-case time complexity is O(n²). An optimized version can achieve O(n) in the best case when the array is already sorted.

4. Does Bubble Sort work with duplicate values?

Yes. Bubble Sort can sort arrays containing duplicate values normally.

5. Can Bubble Sort sort strings?

Yes. The comparison condition can be changed to compare strings alphabetically.

6. What is the purpose of the swapped variable?

The swapped variable helps detect whether any elements were exchanged during a pass. If no swap occurs, the array is already sorted and the algorithm can stop.

7. Is Bubble Sort suitable for large datasets?

Usually, no. Its O(n²) time complexity makes it inefficient for large datasets. More efficient algorithms such as Merge Sort or Quick Sort are generally preferred.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top