Top MCQs on BubbleSort Algorithm with Answers

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4 … More on Bubble Sort

Bubble Sort

Bubble Sort

Question 1

What is the best time complexity of bubble sort(optimised)?

Cross

N^2

Cross

NlogN

Tick

N

Cross

N(logN)^2



Question 1-Explanation: 

The bubble sort is at its best if the input data is sorted. i.e. If the input data is sorted in the same order as expected output. This can be achieved by using one boolean variable. The boolean variable is used to check whether the values are swapped at least once in the inner loop. Consider the following code snippet: 

int main()
{   
    int arr[] = {10, 20, 30, 40, 50}, i, j, isSwapped;
    int n = sizeof(arr) / sizeof(*arr);
    isSwapped = 1;
    for(i = 0; i < n - 1 && isSwapped; ++i)
    {
        isSwapped = 0;
        for(j = 0; j < n - i - 1; ++j)
            if (arr[j] > arr[j + 1])
            {
                swap(&arr[j], &arr[j + 1]);
                isSwapped = 1;
            }
    }
    for(i = 0; i < n; ++i)
        printf(\"%d \", arr[i]);
    return 0;
}

Please observe that in the above code, the outer loop runs only once.

Question 2

Assume that we use Bubble Sort to sort n distinct elements in ascending order. When does the best case of Bubble Sort occur?

Tick

When elements are sorted in ascending order

Cross

When elements are sorted in descending order

Cross

When elements are not sorted by any order

Cross

There is no best case for Bubble Sort. It always takes O(n*n) time



Question 2-Explanation: 

In the best-case scenario, the array is already sorted, but just in case, bubble sort performs O(n) comparisons. As a result, the time complexity of bubble sort in the best-case scenario is O(n).

Hence Option(A) is the correct answer.

Question 3

The number of swappings needed to sort the numbers 8, 22, 7, 9, 31, 5, 13 in ascending order, using bubble sort is

Cross

11

Cross

12

Cross

13

Tick

10



Question 3-Explanation: 

1: 8, 7, 9, 22, 5, 13, 31 = 4 swaps 
2: 7, 8, 9, 5, 13, 22, 31 = 3 swaps 
3: 7, 8, 5, 9, 13, 22, 31 = 1 swap 
4: 7, 5, 8, 9, 13, 22, 31 = 1 swap 
5: 5, 7, 8, 9, 13, 22, 31 = 1 swap 

Total 10 swaps are required to sort the array.

Hence Option(D) is the correct answer.

Question 4

What is the maximum number of comparisons that can take place when a bubble sort algorithm is implemented?, suppose there are n elements in the array.

Cross

n^2

Cross

n (n +1)

Cross

n(n-1)

Tick

(n*(n-1))/2



Question 4-Explanation: 

At pass 1:
Number of comparisons = (N-1)

At pass 2:
Number of comparisons = (N-2)

At pass 3:
Number of comparisons = (N-3)

Likewise,
At pass N-1:
Number of comparisons = 1

Now, calculating total number of comparison required to sort the array
= (N-1) + (N-2) +  (N-3) + . . . 2 + 1
= (N-1)*(N-1+1)/2  { by using sum of N natural Number formula }
= (N * (N-1)) / 2

Total number of comparison (Worst case) = N(N-1)/2

Hence (D) is the correct answer.

Question 5

What is the worst-case time complexity of the Bubble sort Algorithm?

Cross

O(NlogN)

Cross

O(N)

Tick

O(N^2)

Cross

None



Question 5-Explanation: 

The worst-case condition for bubble sort occurs when elements of the array are arranged in decreasing order.
In the worst case, the total number of iterations or passes required to sort a given array is (N-1). where ‘N’ is the number of elements present in the array.

calculating the total number of comparisons required to sort the array
= (N-1) + (N-2) +  (N-3) + . . . 2 + 1
= (N-1)*(N-1+1)/2  { by using sum of N natural Number formula }
= (N * (N-1)) / 2

In the worst case, the Total number of swaps = Total number of comparison
Total number of comparison (Worst case) = N(N-1)/2
Total number of swaps (Worst case) = N(N-1)/2

So worst case time complexity is O(N^2) as N^2 is the highest order term.

Hence (C) is the correct answer.

Question 6

Bubble Sort is an example of a ______ sorting algorithm.

Tick

Comparison - based

Cross

Non-comparison-based

Cross

Linear

Cross

Exponential



Question 6-Explanation: 

Bubble Sort is an example of a comparison-based sorting algorithm. A comparison-based sorting algorithm is a type of sorting algorithm that works by comparing pairs of elements in the input data and swapping them if they are in the wrong order. The goal is to rearrange the elements in ascending or descending order.

Bubble Sort is a comparison-based sorting algorithm because it sorts elements by comparing them pairwise and then making decisions based on those comparisons to rearrange the elements.

Hence Option(A) is the correct answer.

Question 7

Bubble Sort is an example of a sorting algorithm that:

Cross

Uses divide and conquer strategy

Cross

Uses dynamic programming

Tick

Swaps adjacent elements directly

Cross

Only works with integers



Question 7-Explanation: 

Bubble Sort is an example of a sorting algorithm that Swaps adjacent elements directly

void bubbleSort(int arr[], int n)
{
    int i, j;
    bool swapped;
    for (i = 0; i < n - 1; i++) {
        swapped = false;
        for (j = 0; j < n - i - 1; j++) {
        
        //swap adjacent directly
            if (arr[j] > arr[j + 1]) {
                swap(&arr[j], &arr[j + 1]);
                swapped = true;
            }
        }
        if (swapped == false)
            break;
    }
}

Hence (C) is the correct answer.

Question 8

Which of the following statements is true regarding Bubble Sort?

Cross

It is the fastest sorting algorithm.

Cross

It is suitable for sorting large datasets.

Tick

It is an in-place sorting algorithm.

Cross

It has a time complexity of O(n log n) in all cases.



Question 8-Explanation: 

Bubble sort performs the swapping of adjacent pairs without the use of any major data structure. Hence Bubble sort algorithm is an in-place algorithm.

Hence (C) is the correct answer.

Question 9

In Bubble Sort, after each pass, the largest element among the unsorted elements "bubbles up" to which position? You may assume that the elements are being sorted in ascending order.

Cross

First

Tick

Last

Cross

Middle

Cross

Can't Say



Question 9-Explanation: 

In Bubble Sort, after each pass, the largest element among the unsorted elements "bubbles up" to the last position of the current unsorted portion of the array. This means that with each pass, the largest unsorted element is moved to its correct sorted position at the end of the array.

Hence Option(B) is the correct answer.

Question 10

What is the main drawback of the Bubble Sort algorithm?

Cross

 It is not stable. 

Cross

 It is not comparison-based.

Tick

It has a high time complexity. 

Cross

  It is not adaptable to different data types.



Question 10-Explanation: 

Option A: Bubble sort is stable, so this statement is in- correct.
Option B: Bubble sort is a comparison-based algorithm, so this statement is also incorrect.
Option C: Bubble sort has a high time complexity. which is equal to O(n^2).
Option D: It is adaptable to different data types, it will also work for String, Float data types.

Hence option(C) is the correct answer.

There are 10 questions to complete.

 

Coding practice for sorting.

 


  • Last Updated : 27 Sep, 2023

Share your thoughts in the comments
Similar Reads