Data Structure Questions and Answers on Selection Sort for Freshers

1. What is an in-place sorting algorithm?


a) It needs O(1) or O(logn) memory to create auxiliary locations
b) The input is already sorted and in-place
c) It requires additional storage
d) None of the mentioned
Answer: a

Explanation: Auxiliary memory is required for storing the data temporarily.
2. In the following scenarios, when will you use selection sort?

a) The input is already sorted
b) A large file has to be sorted
c) Large values need to be sorted with small keys
d) Small values need to be sorted with large keys
Answer: c

Explanation: Selection is based on keys, hence a file with large values and small keys can be efficiently sorted with selection sort.
3. What is the worst case complexity of selection sort?

a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Answer: d

Explanation: Selection sort creates a sub-list, LHS of the ‘min’ element is already sorted and RHS is yet to be sorted. Starting with the first element the ‘min’ element moves towards the final element.
4. Select the appropriate code that performs selection sort.
a)
        int min;
 for(int j=0; j<arr.length-1; j++)
 {
  min = j;
  for(int k=j+1; k<=arr.length-1; k++)
  {
   if(arr[k] < arr[min])
    min = k;
  }
  int temp = arr[min];
  arr[min] = arr[j];
  arr[j] = temp;
       }
b)
        int min;
 for(int j=0; j<arr.length-1; j++)
 {
  min = j;
  for(int k=j+1; k<=arr.length; k++)
  {
   if(arr[k] < arr[min])
    min = k;
  }
  int temp = arr[min];
  arr[min] = arr[j];
  arr[j] = temp;
 }
c)
        int min;
 for(int j=0; j<arr.length-1; j++)
 {
  min = j;
  for(int k=j+1; k<=arr.length-1; k++)
  {
   if(arr[k] > arr[min])
    min = k;
  }
  int temp = arr[min];
  arr[min] = arr[j];
  arr[j] = temp;
 }
d)
        int min;
 for(int j=0; j<arr.length-1; j++)
 {
  min = j;
  for(int k=j+1; k<=arr.length; k++)
  {
   if(arr[k] > arr[min])
    min = k;
  }
  int temp = arr[min];
  arr[min] = arr[j];
  arr[j] = temp;
 }

Answer: a

Explanation: Starting with the first element as ‘min’ element, selection sort loops through the list to select the least element which is then swapped with the ‘min’ element.

 
5. What is the advantage of selection sort over other sorting techniques?

a) It requires no additional storage space
b) It is scalable
c) It works best for inputs which are already sorted
d) It is faster than any other sorting technique
Answer: a

Explanation: Since selection sort is an in-place sorting algorithm, it does not require additional storage.
6. What is the average case complexity of selection sort?

a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Answer: d

Explanation: In the average case, even if the input is partially sorted, selection sort behaves as if the entire array is not sorted. Selection sort is insensitive to input.
7. What is the disadvantage of selection sort?

a) It requires auxiliary memory
b) It is not scalable
c) It can be used for small keys
d) None of the mentioned
Answer: b

Explanation: As the input size increases, the performance of selection sort decreases.
8. The given array is arr = {3,4,5,2,1}. The number of iterations in bubble sort and selection sort respectively are,

a) 5 and 4
b) 4 and 5
c) 2 and 4
d) 2 and 5
Answer: a

Explanation: Since the input array is not sorted, bubble sort takes 5 iterations and selection sort takes 4(n-1) iterations.
9. The given array is arr = {1,2,3,4,5}. (bubble sort is implemented with a flag variable)The number of iterations in selection sort and bubble sort respectively are,

a) 5 and 4
b) 1 and 4
c) 0 and 4
d) 4 and 1
Answer: b

Explanation: Selection sort is insensitive to input, hence 4(n-1) iterations. Whereas bubble sort iterates only once to set the flag to 0 as the input is already sorted.
10. What is the best case complexity of selection sort?

a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
Answer: d

Explanation: The best, average and worst case complexities of selection sort is O(n2).
(n-1) + (n-2) + (n-3) + …. + 1 = (n(n-1))/2 ~ (n2)/2

Related

C# Questions & Answers on Array and Intialization for Freshers

1. What is the output of the following set of code ? static void Main(string[] args) { int i, j; int[, ] arr = new int[ 3, 3]; for (i = 0; i < 3; ++i) { for ...

C# Questions & Answers on Destructors in Class for Freshers

1. Which operator among the following signifies the destructor operator?a) ::b) :c) ~d) & Answer: c 2. The method called by clients of a class to explicitly release any resources like network,...

C# Questions & Answers on Constructors in Class for Freshers

1. Number of constructors a class can define is ?a) 1b) 2c) Any numberd) None of the mentioned Answer: c Explanation: A constructor is a simple method which has the same name as the class and henc...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item