All Exams Test series for 1 year @ ₹349 only
Question

What is the best case and worst-case complexity of selection sort?

The correct answer is
$O(n^2)$

Question Type: Algorithm Analysis

Step-by-Step Solution:

  • Selection Sort Algorithm: Selection sort works by repeatedly finding the minimum element from the unsorted part of the array and placing it at the beginning.
  • Best-Case Complexity: The best-case scenario occurs when the input array is already sorted. In this case, the algorithm still needs to iterate through the array \(n-1\) times to confirm that it's sorted, making the best-case complexity \(O(n^2)\). While it might seem like it could be \(O(n)\), the nested loops inherent to selection sort prevent this. The algorithm still performs the comparisons.
  • Worst-Case Complexity: The worst-case scenario occurs when the input array is sorted in reverse order. The algorithm will have to make all possible comparisons in the nested loops, resulting in a complexity of \(O(n^2)\).
  • Why other options are incorrect:
    • \(O(n \log n)\): This is the complexity of more efficient sorting algorithms like merge sort and heap sort.
    • \(O(n)\): This is the complexity of linear time algorithms, which selection sort is not.
    • \(O(\log n)\): This is the complexity of algorithms that operate logarithmically, such as binary search.
  • Conclusion: Both the best-case and worst-case time complexities of selection sort are \(O(n^2)\).

"

Was this answer helpful?

Important Questions from Asymptotic Worst Case Time and Time Complexity

  1. Arrange the following in ascending order of their average case complexity.

    Selection-sort, Quick-sort, Linear search, Binary search

  2. Which of the following is correct order of increasing time complexity of algorithms

     A. Tower of Hanoi with n disk. 

    B. Binary search given n sorted numbers. 

    C. Heap sort given n numbers at the worst case. 

    D. Addition of two n x n matrices. 

    Choose the correct answer from the options given below:

  3. Arrange the following algorithms from the most efficient to least efficient based on their time complexity. 

    A. Kruskal's Algorithm

     B. Breadth first search Algorithm 

    C. Bellman-Ford Algorithm

     D. Dijkstra's Algorithm 

    E. Edmonds-Karp Algorithm. 

    Choose the correct answer from the options given below:

  4. What is the average case time complexity of merge sort?
Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App