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

## Is selection sort having same efficiency as bubble sort?

Yes, both have same efficiency order of growth.

## What is the worst case performance of selection sort?

The worst case performance of selection sort is O(n^{2}).

## Selection sort the following array. Show the array after each swap that takes place.{ 30, 60, 20, 50, 40, 10 }

- 30 60 20 50 40 10
**10**60 20 50 40**30**- 10
**20****60**50 40 30 - 10 20
**30**50 40**60** - 10 20 30
**40****50**60 - 10 20 30 40
**50**60 (self-swap) - 10 20 30 40 50
**60**(self-swap)

## Write an algorithm for selection sort.

- Repeat step 2 and 3 varying j from 0 to n-2 //Repeat for n-1 passes
- Find the index of the minimum value in arr[j] to arr[n-1]
- Set min_index=j
- Repeat step c varying i from j+1 to n-1
- If arr[i]<arr[min_index]: min_index = i

- swap arr[j] with arr[min_index]

## Selection sort is suitable for which type of list.

Selection sort is suitable for small list.

## What is the best case performance of selection sort?

The best case performance of selection sort is O(n^{2}).

## What is the worst case space complexity of selection sort?

The worst case space complexity of selection sort is O(n) total, O(1) auxiliary.

## What operation does the Selection Sort use to move numbers from the unsorted section to the sorted section of the list?

The Selection Sort uses the swap operation since it is ordering numbers within a single list.

## Write code in java for selection sort algorithm

public void selectionSort(int[] arr) { int i, j, minIndex, tmp; int n = arr.length; for (i = 0; i < n - 1; i++) { minIndex = i; for (j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } if (minIndex != i) { tmp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tmp; } } } }

## How many comparisons are performed in the second pass of the selection sort algorithm?

n-2 comparisons are performed in the second pass of the selection sort algorithm.

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