# Subjective Interview Questions on Bubble Sort Set 3

## 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. 3 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.

## Write an algorithm for bubble sort.

1. Set pass=1
2. Repeat step 3 varying j from 0 to (n - 1 - pass).
3. If the element at index j is greater than the element at index j+1, swap the two elements.
4. Increment pass by 1.
5. If (pass <= n="" -="" 1)="" go="" to="" step="">

## Write an algorithm for bubble sort.

1. Set pass=1
2. Repeat step 3 varying j from 0 to (n - 1 - pass).
3. If the element at index j is greater than the element at index j+1, swap the two elements.
4. Increment pass by 1.
5. If (pass <= n="" -="" 1)="" go="" to="" step="">

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

Though, array {2, 3, 4, 5, 1} is almost sorted, it takes O(n2) iterations to sort an array. Element {1} is a turtle.

## Sort the following array with bubble sort

Index -> 0 1 2 3 4
arr -> 5 2 6 7 3
In Pass 1, following steps are performed:
1. Compare arr[0] with arr[1]. In the given list, arr[0]>arr[1],therefore, you need to interchange the two values. The resultant list is shown in following fig:
Index -> 0 1 2 3 4
arr -> 2 5 6 7 3
2. Compare arr[1] with arr[2].Here arr[1]<arr[2],therefore, the values remain unchanged.
3. Compare arr[2] with arr[3].Here arr[2]<arr[3] ,therefore, the values remain unchanged.
4. Compare arr[3] with arr[4]. In the given list, arr[3]>arr[4],therefore, you need to interchange the two values. The resultant list is shown in following fig:
Index -> 0 1 2 3 4
arr -> 2 5 6 3 7
At the end of pass 1,the largest element is placed at the last index position. The preceding process will be repeated in all the subsequent passes. In Pass 2, following steps are performed:
1. Compare arr[0] with arr[1].Here arr[0]<arr[1],therefore, the values remain unchanged.
2. Compare arr[1] with arr[2].Here arr[1]<arr[2],therefore, the values remain unchanged.
3. Compare arr[2] with arr[3]. In the given list, arr[2]>arr[3],therefore, you need to interchange the two values. The resultant list is shown in following fig:
Index -> 0 1 2 3 4
arr -> 2 5 3 6 7
At the end of pass 2, the second largest element is placed at its correct position. The preceding process will be repeated in all the subsequent passes. The result after pass 3 would be as follows:
Index -> 0 1 2 3 4
arr -> 2 3 5 6 7
Now the above array is sorted.

O(1) auxiliary.

## What is the order of growth of the bubble sort algorithm?

The bubble sort algorithm has a quadratic order of growth.

## Write Code for bubble sort in C++

```void bubbleSort(int arr[], int n)
{

bool swapped = true;

int j = 0;

int tmp;

while (swapped)     {
swapped = false;

j++;

for (int i = 0; i < n - j; i++)
{

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

tmp = arr[i];

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

arr[i + 1] = tmp;

swapped = true;

}

}

}

```

## While implementing the bubble sort algorithm, how many comparisons will performed in Pass 1?

n - 1 comparisons.

## Bubble sort in C#

```using System;

class AscendingBubbleSort
{

public static void Main()
{

int i = 0,j = 0,t = 0;

int []c=new int[20];

for(i=0;i<20;i++)
{

Console.WriteLine("Enter Value p[{0}]:", i);

}

// Sorting: Bubble Sort
for(i=0;i<20;i++)
{

for(j=i+1;j<20;j++)
{

if(c[i]>c[j])
{

Console.WriteLine(
"c[{0}]={1}, c[{2}]={3}",
i, c[i], j, c[j]);

t=c[i];

c[i]=c[j];

c[j]=t;

}

}

}

Console.WriteLine("Here comes the sorted array:");

// Print the contents of the sorted array
for(i=0;i<20;i++)
{

Console.WriteLine ("c[{0}]={1}", i, c[i]);

}

}

}

```

## What is the order of growth of the bubble sort algorithm?

The bubble sort algorithm has a quadratic order of growth.

## 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.