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

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

## Write an algorithm for bubble sort.

- Set pass=1
- Repeat step 3 varying j from 0 to (n - 1 - pass).
- If the element at index j is greater than the element at index j+1, swap the two elements.
- Increment pass by 1.
- 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(n^{2}) 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 |

- 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 - Compare arr[1] with arr[2].Here arr[1]<arr[2],therefore, the values remain unchanged.
- Compare arr[2] with arr[3].Here arr[2]<arr[3] ,therefore, the values remain unchanged.
- 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

- Compare arr[0] with arr[1].Here arr[0]<arr[1],therefore, the values remain unchanged.
- Compare arr[1] with arr[2].Here arr[1]<arr[2],therefore, the values remain unchanged.
- 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

Index -> | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|

arr -> | 2 | 3 | 5 | 6 | 7 |

## What is worst case space complexity of bubble sort algorithm?

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); c[i] = int.Parse(Console.ReadLine()); } // 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.