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.
What is the mathematical definition of selection sort?
We define selection sort mathematically:
Let L be a non-empty set and such that f(L) = L' where:- L' is a permutation of L,
- for all and
- s is the smallest element of L, and
- Ls is the set of elements of L without one instance of the smallest element of L.
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]
How is selection sort implemented ?
void selectionSort(int numbers[], int array_size) { int i, j; int min, temp; for (i = 0; i < array_size-1; i++) { min = i; for (j = i+1; j < array_size; j++) { if (numbers[j] < numbers[min]) { min = j; } } temp = numbers[i]; numbers[i] = numbers[min]; numbers[min] = temp; } }
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.
What is the best case performance of selection sort?
The best case performance of selection sort is O(n2).
What is the mathematical definition of selection sort?
We define selection sort mathematically:
Let L be a non-empty set and such that f(L) = L' where:- L' is a permutation of L,
- for all and
- s is the smallest element of L, and
- Ls is the set of elements of L without one instance of the smallest element of L.
How do you define selection sort?
Selection sort scans through the list iteratively, selecting one item in each scan, and moving the item to its correct position in the list.
NOTE: Iteratively means one by one. It can also be called 'in a loop'.
Which are the basic steps for performing selection sort?
There are basically two steps perform during selection sort:
- Find smallest element in unsorted portion from list or array.
- Interchange the smallest element with the one at the front of the unsorted portion
How can we rewrite the Selection Sort algorithm so that it sorts numbers from highest to lowest?
To modify the Selection Sort Algorithm so that it sorts from highest to lowest, we need to search for the largest card rather than the smallest. The modifications to the original algorithm are marked in italics.
- Get a list of unsorted numbers
- Set a marker for the unsorted section at the front of the list
- Repeat steps 4 through 7 until one number remains in the unsorted section
- Compare all unsorted numbers
- Select the largest unsorted number
- Swap this number with the first number in the unsorted
- Advance the marker to the right one position
- Stop
How is Selection Sort analyzed?
Selection sort is not difficult to analyze compared to other sorting algorithms since none of the loops depend on the data in the array. Selecting the lowest element requires scanning all n elements (this takes n - 1 comparisons) and then swapping it into the first position. Finding the next lowest element requires scanning the remaining n - 1 elements and so on, for comparisons. Each of these scans requires one swap for (n-1) elements.
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.