Selection Sort Questions for Interview Preparation Set 3

Interview questions on selection sort in data structures. They are in a brief dossier form. Quick answers have been given to each question. Please inform us if you find any mistakes. 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.

How many Comparisons would be Required?

Consider the following lists of partially sorted numbers. The double bars represent the sort marker. For each list state how many comparisons and swaps are needed to sort the next number.

  1. [1 3 4 6 7 || 9 8]
  2. [1 2 4 5 8 || 9]
  3. [1 3 || 4 9 6 8
  4. [|| 9 2 3 8 1 6 4]
  5. [2 3 4 || 9 8 7 6 5]

Answer:

  1. comparisons = 0, swaps = 1
  2. comparisons = 0, swaps = 0
  3. comparisons = 3, swaps = 0
  4. comparisons = 6, swaps = 1
  5. comparisons = 4, swaps = 1

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.

Sort an Array with C++

{

    30, 50, 20, 10, 40
}

THIS IS A SCHEMATIC SOLUTION
const int nSize = 5;

int anArray[nSize] = { 30, 50, 20, 10, 40 };

void main()
{

    // Step through each element of the array
    for (int nStartIndex = 0; nStartIndex < nSize; nStartIndex++)
    {

        // nSmallestIndex is the index of the smallest element
        // we've encountered so far.
        int nSmallestIndex = nStartIndex;

        // Search through every element starting at nStartIndex+1
        for (
        int nCurrentIndex = nStartIndex + 1;

        nCurrentIndex < nSize;

        nCurrentIndex++
        )
        {

            // If the current element is smaller
            // than our previously found smallest
            if (anArray[nCurrentIndex] < anArray[nSmallestIndex])
            // Store the index in nSmallestIndex
            {

                nSmallestIndex = nCurrentIndex;

            }

        }

        // Swap our start element with our smallest element
        swap(anArray[nStartIndex], anArray[nSmallestIndex]);

    }

}


Another Variation of the above question: Rewrite the selection sort code above to sort in descending order (largest numbers first).

Simply change:
If (anArray[nCurrentIndex] < anArray[nSmallestIndex])
to
if (anArray[nCurrentIndex] > anArray[nSmallestIndex])
nSmallestIndex should probably be renamed nLargestIndex as well.
const int nSize = 5;

int anArray[nSize] = { 30, 50, 20, 10, 40 };

void main()
{

    // Step through each element of the array
    for (int nStartIndex = 0; nStartIndex < nSize; nStartIndex++)
    {

        // nLargestIndex is the index of the smallest element
        // we've encountered so far.
        int nLargestIndex = nStartIndex;

        // Search through every element starting at nStartIndex+1
        for (
        int nCurrentIndex = nStartIndex + 1;

        nCurrentIndex <

        nSize; nCurrentIndex++
        )
        {

            // If the current element is smaller
            // than our previously found smallest
            if (anArray[nCurrentIndex] < anArray[nLargestIndex])
            {

                // Store the index in nLargestIndex
                nLargestIndex = nCurrentIndex;

            }

        }

        // Swap our start element with our smallest element
        swap(anArray[nStartIndex], anArray[nLargestIndex]);

    }

}


What is the average case performance of selection sort?

The average case performance of selection sort is O(n2).

What is the worst case performance of selection sort?

The worst case performance of selection sort is O(n2).

Is selection sort having same efficiency as bubble sort?

Yes, both have same efficiency order of growth.

What is the average case performance of selection sort?

The average case performance of selection sort is O(n2).

Sort the following array

{

    30, 50, 20, 10, 40
}


Answer:

  1. We find the smallest element, starting from index 0:

    { 30, 50, 20, 10 , 40 }

  2. We then swap this with the element at index 0:

    { 10 , 50, 20, 30 , 40 }

  3. Now that the first element is sorted, we can ignore it. Consequently, we find the smallest element, starting from index 1:

    { 10, 50, 20 , 30, 40 }

  4. And swap it with the element in index 1:

    { 10, 20 , 50 , 30, 40 }

  5. Find the smallest element starting at index 2:

    { 10, 20, 50, 30 , 40 }

  6. And swap it with the element in index 2:

    { 10, 20, 30 , 50 , 40 }

  7. Find the smallest element starting at index 3:

    { 10, 20, 30, 50, 40  }

  8. And swap it with the element in index 3:

    { 10, 20, 30, 40 , 50  }

  9. Finally, find the smallest element starting at index 4:

    { 10, 20, 30, 40, 50  }

  10. And swap it with the element in index 4 (which doesn't do anything):

    { 10, 20, 30, 40, 50  }

  11. Done!

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

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.

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.