# Subjective Interview Questions on Bubble Sort Set 1

## Subjective, long answer type interview questions for freshers on bubble sort. These questions have been asked in interviews for MNCs like Wipro, TCS and Infosys. This is subjective type short answers question and answer set no. 1 in this series.

Last Reviewed and Updated on February 7, 2020
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

## Interview and Exam Questions and Answers

This set contains a list of commonly asked questions. They are short interview questions aimed at freshers interview, campus placement drives, and also for job interviews. You can use these to have a quick grasp and brush-up of your fundamentals. These questions can be viewed on a mobile phone also because this website is built on responsive web design.

## How do we define bubble sort?

Bubble sort repeatedly scan the list, comparing adjacent elements swapping them if they are in wrong order.

## How can the efficiency of bubble sort algorithm be determined?

The efficiency of bubble sort algorithm can be determined in terms of the number of comparisons.

## What would happen if bubble sort didn't keep track of the number of swaps made on each pass through the list?

The algorithm wouldn't know when to terminate as it would have no way of knowing when the list was in sorted order.

## How do we define bubble sort?

Bubble sort repeatedly scan the list, comparing adjacent elements swapping them if they are in wrong order.

## What main properties are considered in bubble sort?

1. Stable
2. O(1) extra space
3. O(n2) comparisons and swaps
4. Adaptive: O(n) when nearly sorted

## What is the complexity of the bubble sort algorithm?

The complexity is O(n2). The time required to execute the bubble sort algorithm is proportional to (n2), where n is the number of input items.

## Write Code for bubble sort in java

```public void bubbleSort(int[] arr)
{

boolean swapped = true;

int j = 0;

int tmp;

while (swapped)
{

swapped = false;

j++;

for (int i = 0; i < arr.length - j; i++)
{

if (arr[i] > arr[i + 1])
{

tmp = arr[i];

arr[i] = arr[i + 1];

arr[i + 1] = tmp;

swapped = true;

}

}

}

}

```

## What simple modification could be made to the bubble sort algorithm that would make it perform more efficiently on the case described above?

The algorithm could be made to start at the end of the list and move towards the front, swapping elements only if the first is greater than the second. In this way, the smallest numbers would bubble down instead of the large numbers bubbling up. Note however that this algorithm has its own worst case scenario that is just as bad as the worst case for the normal bubble sort. It occurs when the list's largest element is in the first position.

## What are the basic criteria for selecting appropriate algorithm?

1. Execution Time.
2. Storage Space.
3. Programming Effort.

## Give an example of Rabbit problem in bubble sort.

Array {6, 1, 2, 3, 4, 5} is almost sorted, but it takes O(n) iterations to sort it. Element {6} is a rabbit.

## My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.