Open In App

Time and Space complexity analysis of Selection Sort

Last Updated : 22 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Selection sort algorithm has a time complexity of O(n^2) and a space complexity of O(1) since it does not require any additional memory space apart from a temporary variable used for swapping.

Time Complexity Analysis of Selection Sort:

  • Best-case: O(n2), best case occurs when the array is already sorted. (where n is the number of integers in an array)
  • Average-case: O(n2), the average case arises when the elements of the array are in a disordered or random order, without a clear ascending or descending pattern.
  • Worst-case: O(n2), The worst-case scenario arises when we need to sort an array in ascending order, but the array is initially in descending order.

The time complexity of the Selection sort remains constant regardless of the input array’s initial order. At each step, the algorithm identifies the minimum element and places it in its correct position. However, the minimum element cannot be determined until the entire array is traversed.

Breakdown for Time Complexity for Selection Sort:

Number of iterations                        

Comparisons                                           

1st

(n-1)

2nd

(n-2)

3rd

(n-3)

……..

……..

last

1

  • The number of comparisons in selection sort can be calculated as the sum of (n-1) + (n-2) + (n-3) + ... + 1   , which is approximately equal to n(n-1)/2    or roughly n^2. This results in a time complexity of O(n^2). 
  • Another way to analyze the complexity is by observing the number of loops, which in selection sort is two nested loops. Hence, the overall complexity is proportional to n^2.

Auxiliary Space Complexity Analysis of Selection Sort:

  • Space Complexity: O(1), as no extra space is required for the Selection sort algorithm

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads